| | |
passing variables from php/html to java scripts
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
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):
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?
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)
<HTML> <HEAD> <script language="JavaScript"> function ConfirmForm(N) { var cmp = N; IF ( cmp != "" ) { return confirm("are you sure you want to submit this?"); } ELSE { return alert("You have not fild all of the necesary fields"); } } </script> </HEAD> <BODY> ECHO "<FORM NAME=zzz METHOD=get ACTION=something.php TARGET='lll' OnSubmit='return ConfirmForm(N);'>"; ECHO "Name: <INPUT TYPE='text' NAME=N ID=N/>"; ECHO "<INPUT TYPE = submit NAME = button_submit VALUE = 'Submit'>"; </FORM> </BODY> </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?
it much easier just to use php
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.
PHP Syntax (Toggle Plain Text)
<?php //this is just an example if(empty($_POST['N'])){ $message = 'Please fill in your Name'; } if($message){ echo $message; } else { //insert into database } ?>
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.
•
•
Join Date: Oct 2008
Posts: 42
Reputation:
Solved Threads: 0
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:
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?
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)
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?
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:
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:
You are also mixing html and xhtml elements, no doctype...
Here's a cleaner version of your page:
html Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> function ConfirmForm() { var cmp = document.zzz.N.value; if (cmp != "") { return confirm("are you sure you want to submit this?"); } else { return alert("You have not filled all of the necesary fields"); } } </script> <title>Form</title> </head> <body> <form name="zzz" method="get" action="something.php" onsubmit='return ConfirmForm();'> Name: <input type='text' name="N" id="N" /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>
php Syntax (Toggle Plain Text)
<?php if(empty($_GET['N'])){ header('Location:form.php'); exit; } ?> <html> <head>
Lost time is never found again.
- Benjamin Franklin
- Benjamin Franklin
•
•
Join Date: Dec 2008
Posts: 20
Reputation:
Solved Threads: 1
php Syntax (Toggle Plain Text)
<html> <body> <?php $display_form = true; if($_POST['submit']=="Submit") { if(trim($_POST['my_text'])=="") { $error = "Please fill text box"; } else { echo ' <form name="my_form" id="my_form" method="POST" action="submit.php"> <input type="hidden" name="my_text" value="'.$_POST['my_text'].'"> </form> <script language="JavaScript"> function form_submit() { var my_frm = document.getElementById("my_form"); my_frm.submit(); } window.onload=form_submit; </script>'; $display_form = false; } }//EO if($_POST['submit']=="Submit") echo "<br><font color='#FF0000'>".$error."</font><br>"; if($display_form == true) { ?> <form name="same_page" method="POST"> <input type="text" name="my_text" value="<?php echo $_POST['my_text']; ?>"> <input type="submit" name="submit" value="Submit"> </form> <?php } ?> </body> </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.
![]() |
Similar Threads
- php.ini confusion (PHP)
Other Threads in the PHP Forum
- Previous Thread: Problem installing MySQL with apache and php
- Next Thread: mysql escape string
| Thread Tools | Search this Thread |
# address apache api array autoincrement beginner binary broken cakephp checkbox class clean cms code countingeverycharactersfromastring crack cron curl database date decode dehasher directory display dissertation dynamic echo email error fairness file files folder form forms function functions google href htaccess html image include incode insert ip javascript joomla legislation limit link login mail masterthesis match menu method mlm multiple mysql newsletters oop pageing paypal pdf persist php play protocol query question radio random remote root script search server sessions simple sms soap source space spam sql support! syntax system table tutorial update upload url validator variable video web youtube





