elmas156 Posted September 15, 2008 Share Posted September 15, 2008 Hey guys, I'm trying to validate a form using javascript and I have everything working except for one checkbox. What I would like to happen is, if the checkbox is not checked then pop up an alert to say that it has to be checked before continuing. I've tried several different ways with javascript but the form goes through with no alerts regardless of whether the checkbox is checked or not. Now I'm wondering if there is any way to accomplish what I need to do with php and if so, where do I start? If not what would make this work the way I'm trying it? Here's what I've tried: <head> <script language="javascript"> function validate(form) { if (form.accept.value == "true") { alert("You must accept the Terms of Service before continuing."); form.accept.focus(); return false; } } </script> </head> <body> <?php echo "<form action=\"signup.php\" method=\"POST\">"; echo "<input name=\"accept\" type=\"checkbox\" id=\"accept\" value=\"true\">"; echo "<input name=\"submit\" type=\"submit\" value=\"I accept, continue to my account.\" onClick=\"return validate(form)\">"; echo "</form>"; </body> Link to comment https://forums.phpfreaks.com/topic/124253-solved-validate-form-with-checkbox/ Share on other sites More sharing options...
genericnumber1 Posted September 15, 2008 Share Posted September 15, 2008 This is the wrong forum for javascript. but... http://www.google.com/search?q=checkbox+form+validation+with+javascript Link to comment https://forums.phpfreaks.com/topic/124253-solved-validate-form-with-checkbox/#findComment-641651 Share on other sites More sharing options...
elmas156 Posted September 15, 2008 Author Share Posted September 15, 2008 I understand that this is not the javascript forum. My question is how can this same effect be accomplished using php? Though it's not common, users can disable javascript on their browsers and get around a javascript form validation, making it obsolete. I figured out how to make it work with javascript but I would rather use something that wouldn't be so easy to disable. Thanks for your help though. Link to comment https://forums.phpfreaks.com/topic/124253-solved-validate-form-with-checkbox/#findComment-641667 Share on other sites More sharing options...
unrelenting Posted September 15, 2008 Share Posted September 15, 2008 I understand that this is not the javascript forum. My question is how can this same effect be accomplished using php? Though it's not common, users can disable javascript on their browsers and get around a javascript form validation, making it obsolete. I figured out how to make it work with javascript but I would rather use something that wouldn't be so easy to disable. Thanks for your help though. The only way I know of is to check if it's set on the signup page. if (!isset($_POST['checkboxname'])) Link to comment https://forums.phpfreaks.com/topic/124253-solved-validate-form-with-checkbox/#findComment-641669 Share on other sites More sharing options...
genericnumber1 Posted September 15, 2008 Share Posted September 15, 2008 I misread your question, I have a habit of skipping last sentences, sorry. In the simplest form... if(empty($_POST['accept'])) { echo "Check box not checked!"; } That should give some sort of a starting point. Google "php form validation" for tons of info about various types of validation. Link to comment https://forums.phpfreaks.com/topic/124253-solved-validate-form-with-checkbox/#findComment-641670 Share on other sites More sharing options...
venkat-paypal Posted September 15, 2008 Share Posted September 15, 2008 Hi, As now your code is good, but you need to do some change in the Javascript code like below Solution: You just change the condition from if (form.accept.value == "true") to, if (!form.accept.checked) This is enough to meet your requirment. Please try and let me know if any question Example: I just tried this as normal html page (test.html) <html> <head> <script language="javascript"> function validate(form) { if (!form.accept.checked) { alert("You must accept the Terms of Service before continuing."); form.accept.focus(); return false; } } </script> </head> <body> <form action="signup.php" method="POST"> <input name="accept" type="checkbox" id="accept" value="true"> <input ame="submit" type="submit" value="I accept, continue to my account." onClick="return validate(form)"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/124253-solved-validate-form-with-checkbox/#findComment-641760 Share on other sites More sharing options...
elmas156 Posted September 16, 2008 Author Share Posted September 16, 2008 It works now, thank you. Link to comment https://forums.phpfreaks.com/topic/124253-solved-validate-form-with-checkbox/#findComment-642542 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.