943,716 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2590
  • PHP RSS
Dec 20th, 2008
0

passing variables from php/html to java scripts

Expand Post »
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):

PHP Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
marcmm is offline Offline
42 posts
since Oct 2008
Dec 20th, 2008
0

Re: passing variables from php/html to java scripts

it much easier just to use php

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 14
Solved Threads: 21
Posting Whiz
compdoc is offline Offline
325 posts
since Mar 2008
Dec 20th, 2008
0

Re: passing variables from php/html to java scripts

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:

PHP Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
marcmm is offline Offline
42 posts
since Oct 2008
Dec 20th, 2008
0

Re: passing variables from php/html to java scripts

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:
html Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  1. <?php
  2. if(empty($_GET['N'])){
  3. header('Location:form.php');
  4. exit;
  5. }
  6. ?>
  7. <html>
  8. <head>
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Dec 21st, 2008
0

Re: passing variables from php/html to java scripts

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 5
Light Poster
manish.s is offline Offline
29 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Problem installing MySQL with apache and php
Next Thread in PHP Forum Timeline: mysql escape string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC