ded Posted January 13, 2009 Share Posted January 13, 2009 FormCheck not working <head> <script language="JavaScript"> function checkForm() { if($company) == '') { alert('Please enter your Company Name); return false; } } </script> </head> <form action="post.php" onSubmit="return checkForm(this)" method="POST"> <table align=center width="575" border=0 cellspacing=10> <tr> <td align=right><b>Name of Company</b> </td> <td><input type="text" name="company" size="50"> </td> </tr> <tr> <td align=center colspan="2"><input type="submit" name="Submit" value="Submit Form"> </td> </tr> </table> Any ideas? Regards, DED Quote Link to comment Share on other sites More sharing options...
revraz Posted January 13, 2009 Share Posted January 13, 2009 Where do you grab $_POST['company']? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted January 13, 2009 Share Posted January 13, 2009 FormCheck not working <head> <script language="JavaScript"> function checkForm() { if($company) == '') { alert('Please enter your Company Name); return false; } } </script> </head> <form action="post.php" onSubmit="return checkForm(this)" method="POST"> <table align=center width="575" border=0 cellspacing=10> <tr> <td align=right><b>Name of Company</b> </td> <td><input type="text" name="company" size="50"> </td> </tr> <tr> <td align=center colspan="2"><input type="submit" name="Submit" value="Submit Form"> </td> </tr> </table> Any ideas? Regards, DED First of all, this is a JavaScript validator you're trying to write, not a PHP one. Secondly, you put "return checkForm(this)" in the HTML of the form, but your function doesn't accept an argument (i.e., it's written as "checkForm(){ /*... */}"). Third, where does $company come from? If it's supposed to be from the PHP script, then, put simply, you're doing it wrong. JavaScript execution always occurs before the page is reloaded, and the form values are sent to the PHP script. So, right now, $company = '' If $company is supposed to be supplied by the form itself to the JavaScript, then, again, you're doing it wrong. You need to grab a hold of that form field, then retrieve its value. Here's the best solution, assuming your validator is JavaScript-only: <html> <head> <title>blah</title> <script type="text/javascript"> window.onload = function() { var myForm = document.forms['myForm']; myForm.onsubmit = function() { if(this.elements['company'].value == '') { alert("Please enter company name"); return false; } else { return true; } } } </script> </head> <body> <form name="myForm" action="post.php" method="POST"> <table align=center width="575" border=0 cellspacing=10> <tr> <td align=right><b>Name of Company</b> </td> <td><input type="text" name="company" size="50"> </td> </tr> <tr> <td align=center colspan="2"><input type="submit" name="Submit" value="Submit Form"> </td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
ded Posted January 13, 2009 Author Share Posted January 13, 2009 Nightslyr....thank you for your help with this. Did forms before, but never validated it. One more problem maybe you can help me with: I have this: <input type="checkbox" name="iagree" value="Y"> <left><strong>I Agree</strong></left> and this: else if(this.elements['iagree'].value != 'Y') { alert("Please check the I AGREE box to accept the terms"); return false; } The form is process whether or not the box is checked. Regards, DED Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 13, 2009 Share Posted January 13, 2009 A checkbox value is the same whether the checkbox is checked or not. You need to test if the field is checked - not the value: else if(!this.elements['iagree'].checked) { alert("Please check the I AGREE box to accept the terms"); return false; } Quote Link to comment 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.