passing variables from php/html to java scripts

Reply

Join Date: Oct 2008
Posts: 42
Reputation: marcmm is an unknown quantity at this point 
Solved Threads: 0
marcmm marcmm is offline Offline
Light Poster

passing variables from php/html to java scripts

 
0
  #1
Dec 20th, 2008
ok, I have a form that submits data to a database. data that is colected from things like dropdown boxes, textboxes, checkboxes etc.

I want to put some conditions that prevent the user from submitting something that is missing important data.

I found a little tutorial that dose that, but it dosen't quite work because I neet to transfer a value from a thextbox to a function and make a check that it's not empty.

here's an example of what I would like to do ( the code obviously dose not work as is):

  1.  
  2. <HTML>
  3. <HEAD>
  4. <script language="JavaScript">
  5.  
  6. function ConfirmForm(N)
  7. {
  8. var cmp = N;
  9.  
  10. IF ( cmp != "" )
  11. {
  12. return confirm("are you sure you want to submit this?");
  13. }
  14. ELSE
  15. {
  16. return alert("You have not fild all of the necesary fields");
  17. }
  18. }
  19.  
  20. </script>
  21. </HEAD>
  22. <BODY>
  23.  
  24. ECHO "<FORM NAME=zzz METHOD=get ACTION=something.php TARGET='lll' OnSubmit='return ConfirmForm(N);'>";
  25.  
  26. ECHO "Name: <INPUT TYPE='text' NAME=N ID=N/>";
  27.  
  28. ECHO "<INPUT TYPE = submit NAME = button_submit VALUE = 'Submit'>";
  29.  
  30. </FORM>
  31.  
  32. </BODY>
  33. </HTML>

I don't know how to pass value of the text box from php to the java script.

Normally I would imagine there would need to be a $GET[N] statement. but I don't know where to post it. don't think it works inside the script itself.

Can anybody tell me how to do this?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 323
Reputation: compdoc is an unknown quantity at this point 
Solved Threads: 21
compdoc's Avatar
compdoc compdoc is offline Offline
Posting Whiz

Re: passing variables from php/html to java scripts

 
0
  #2
Dec 20th, 2008
it much easier just to use php

  1.  
  2. <?php
  3. //this is just an example
  4. if(empty($_POST['N'])){
  5. $message = 'Please fill in your Name';
  6. }
  7. if($message){
  8. echo $message;
  9. }
  10. else {
  11. //insert into database
  12. }
  13. ?>

when the form is submitted you can send to a php page that checks the form or you can submit the page to itself to check all the fields and check boxes. and then either echo the the error message or process the page and insert all the data to the database

edit: I forgot to add that some people might have javascript turned off in their browser, and if thats the case then the javascript used to check the form won't work.
Last edited by compdoc; Dec 20th, 2008 at 12:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 42
Reputation: marcmm is an unknown quantity at this point 
Solved Threads: 0
marcmm marcmm is offline Offline
Light Poster

Re: passing variables from php/html to java scripts

 
0
  #3
Dec 20th, 2008
I can't resubmit the page because I'm already using the submit function to send the data to another page.

the intermediar check.php page seemsi nteresting. butr I ahve no idea how to make the page automaticly redirect the user to either the submit page or back to the data entry page.

any ideas how to automaticly redirect users without them touching anything?

I know there's something like this used for a back to previous page link:

  1. onClick='history.go(-1);return true;'

and that would be perfect for sending the user back to the previous page after stating what he needs to do to continue. but that code requires that the viewer push a button. wich I don't have ( too bad I can't link the ok button from the alert message that I would use to point out the errores.

and then there's the problem with redirecting him to the submit.php page in case all the info is corect.

how do you automaticly redirect to certain pages?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: passing variables from php/html to java scripts

 
0
  #4
Dec 20th, 2008
Your code is a complete mess. You're trying to echo static html and it doesn't have any <?php delimiters. The delimiters tell the server which parts of the code to parse. Without them, all of your code gets delivered to the browser without being parsed in plain text.
You are also mixing html and xhtml elements, no doctype...
Here's a cleaner version of your page:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <script type="text/javascript">
  6.  
  7. function ConfirmForm()
  8. {
  9. var cmp = document.zzz.N.value;
  10.  
  11. if (cmp != "")
  12. {
  13. return confirm("are you sure you want to submit this?");
  14. }
  15. else
  16. {
  17. return alert("You have not filled all of the necesary fields");
  18. }
  19. }
  20.  
  21. </script>
  22. <title>Form</title>
  23. </head>
  24. <body>
  25. <form name="zzz" method="get" action="something.php" onsubmit='return ConfirmForm();'>
  26.  
  27. Name: <input type='text' name="N" id="N" />
  28. <input type="submit" name="submit" value="Submit" />
  29.  
  30. </form>
  31.  
  32. </body>
  33. </html>
Your javascript validation will work fine to save the user time. However, it's not going to be secure. You need to do server side validation. At the top of something.php, do some basic checks and redirect back to the referring page if any fields aren't complete:
  1. <?php
  2. if(empty($_GET['N'])){
  3. header('Location:form.php');
  4. exit;
  5. }
  6. ?>
  7. <html>
  8. <head>
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 20
Reputation: manish.s is an unknown quantity at this point 
Solved Threads: 1
manish.s manish.s is offline Offline
Newbie Poster

Re: passing variables from php/html to java scripts

 
0
  #5
Dec 21st, 2008
  1. <html>
  2. <body>
  3.  
  4. <?php
  5.  
  6. $display_form = true;
  7.  
  8. if($_POST['submit']=="Submit")
  9. {
  10. if(trim($_POST['my_text'])=="")
  11. {
  12. $error = "Please fill text box";
  13. }
  14. else
  15. {
  16. echo ' <form name="my_form" id="my_form" method="POST" action="submit.php">
  17.  
  18. <input type="hidden" name="my_text" value="'.$_POST['my_text'].'">
  19.  
  20. </form>
  21.  
  22. <script language="JavaScript">
  23. function form_submit()
  24. {
  25. var my_frm = document.getElementById("my_form");
  26. my_frm.submit();
  27. }
  28. window.onload=form_submit;
  29.  
  30. </script>';
  31.  
  32. $display_form = false;
  33. }
  34.  
  35. }//EO if($_POST['submit']=="Submit")
  36.  
  37. echo "<br><font color='#FF0000'>".$error."</font><br>";
  38.  
  39. if($display_form == true)
  40. {
  41. ?>
  42.  
  43.  
  44. <form name="same_page" method="POST">
  45. <input type="text" name="my_text" value="<?php echo $_POST['my_text']; ?>">
  46. <input type="submit" name="submit" value="Submit">
  47. </form>
  48.  
  49. <?php
  50. }
  51. ?>
  52.  
  53. </body>
  54. </html>


Note : This an example of SERVER side validation, you can add CLIENT side validation also.
Last edited by peter_budo; Dec 21st, 2008 at 4:48 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC