edzillion Posted February 1, 2008 Share Posted February 1, 2008 I have a form on my site that needs validating: <form id="details" onsubmit="return validateFormOnSubmit(this)" name="updates" method="post" action="details_upd.php"> At first I was using the javascript function validateFormOnSubmit() to do the validation, but I wanted to have a backup plan in case the user didn't have javascript enabled, so I added some form validation to the details_upd.php file. I thought that the javascript would run first and then the php, but it seems to be ignoring the validateFormOnSubmit() now that I added the php validation. Is there a way I can get it to run the function, and only run the php validation if javascript is disabled? TIA Ed Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/ Share on other sites More sharing options...
Lumio Posted February 1, 2008 Share Posted February 1, 2008 hm... if everything is fine, then php should also validate the same. Can you give some more code please? Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455122 Share on other sites More sharing options...
pdkv2 Posted February 1, 2008 Share Posted February 1, 2008 Javascript validation will be ignored only if javascript is disabled otherwise it will work, But php validation will work in both cases. Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455125 Share on other sites More sharing options...
edzillion Posted February 1, 2008 Author Share Posted February 1, 2008 Yes, but which runs first? I want 1. First javascript validation: function validateFormOnSubmit(theForm) { var reason = ""; reason += validateEmpty(theForm.from); reason += validateEmail(theForm.email); if (reason != "") { alert("Some fields need correction:\n" + reason); return false; } return true; } 2. Then php validation in if(((!isset($_POST["name"])) || ($_POST["name"] == "")) || ((!isset($_POST["email"])) || ($_POST["email"] == ""))) { header("Location: http://www.goldassets.co.uk/market-updates-problem.php"); exit(); } if(!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $_POST["email"])) { header("Location: http://www.goldassets.co.uk/market-updates-problem.php"); exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455126 Share on other sites More sharing options...
pdkv2 Posted February 1, 2008 Share Posted February 1, 2008 Javascript will always run first because it runs on the client machine where as php runs on the server. Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455130 Share on other sites More sharing options...
Lumio Posted February 1, 2008 Share Posted February 1, 2008 Javascript will always run first because it runs on the client machine where as php runs on the server. I don't think so. If edzillions page runs the page as details....php and on that page he outputs the form and checks the values. Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455132 Share on other sites More sharing options...
pdkv2 Posted February 1, 2008 Share Posted February 1, 2008 Where is your validateEmpty() function ? Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455146 Share on other sites More sharing options...
edzillion Posted February 1, 2008 Author Share Posted February 1, 2008 Where is your validateEmpty() function ? In the same javascript file: <script src="js/validation.js" type="text/javascript" language="JavaScript" ></script> The function itself: function validateEmpty(fld) { var error = ""; if (fld.value.length == 0) { fld.style.background = 'Yellow'; error = "You didn't enter a name.\n" } else { fld.style.background = 'White'; } return error; } Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455157 Share on other sites More sharing options...
pdkv2 Posted February 1, 2008 Share Posted February 1, 2008 So you are passing "theForm.from" to the function "validateEmpty()" but you should pass "theForm.name" Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455168 Share on other sites More sharing options...
edzillion Posted February 1, 2008 Author Share Posted February 1, 2008 The script works fine. I'll repeat my original question: How can I get the validateFormOnSubmit() function to run before the details_upd.php validation runs? Thanks Ed Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455172 Share on other sites More sharing options...
pdkv2 Posted February 1, 2008 Share Posted February 1, 2008 That is what i am suggesting you must be having some problem in validateFormOnSubmit() function thats why it is not working first where as it should work first just try to put only alert message in this function it will work !! Cheers ! Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455177 Share on other sites More sharing options...
haku Posted February 1, 2008 Share Posted February 1, 2008 I don't think so. Sorry, but you are mistaken on that. Javascript will always run first. As to the original question - you have a mistake somewhere in your javascript. If your javascript fails, it will default to the php (which is one reason you should never rely only on javascript validation, which you seem to have realized). Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455226 Share on other sites More sharing options...
edzillion Posted February 1, 2008 Author Share Posted February 1, 2008 hey thanks man, that worked a treat! Ed Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455244 Share on other sites More sharing options...
pdkv2 Posted February 1, 2008 Share Posted February 1, 2008 so plz tell me what was the problem ? Quote Link to comment https://forums.phpfreaks.com/topic/88861-solved-form-validation-in-php-javascript-question/#findComment-455245 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.