DrTrans Posted January 16, 2012 Share Posted January 16, 2012 print "<script type=\"text/javascript\">"; print "function validateForm() { var x=document.forms[\"addp\"][\"input_address\"].value; if (x==null || x==\"\") { alert(\"You must enter an address.\"); return false; } var x=document.forms[\"addp\"][\"input_city\"].value; if (x==null || x==\"\") { alert(\"You must enter a city.\"); return false; } var x=document.forms[\"addp\"][\"input_state\"].value; if (x==null || x==\"\") { alert(\"You must enter a state.\"); return false; } var x=document.forms[\"addp\"][\"input_zip\"].value; if (x==null || x==\"\") { alert(\"You must enter a zip code\"); return false; } var x=document.forms[\"addp\"][\"input_rent\"].value; if (x==null || x==\"\") { alert(\"You must enter an address.\"); return false; } } "; print"</script>"; Quote Link to comment https://forums.phpfreaks.com/topic/255147-is-there-a-simpler-way-to-do-this/ Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 1. to avoid silly errors that come from having to escape everything, don't use php to echo javascript, end php, execute the javascript, then start php again <?php echo "blah"; ?> <script> //js stuff </script> <?php ?> 2. well, you could pass the form object values as arguments, which would make for a more robust and reusable function, which is what functions are for, but either way you should definitely be assigning each value to a different variable. 3. you can for loop the document.forms['addp'] values and check for them being null or empty inside of the loop, this would reduce the amount of code needed greatly. Quote Link to comment https://forums.phpfreaks.com/topic/255147-is-there-a-simpler-way-to-do-this/#findComment-1308231 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.