jjmusicpro Posted April 16, 2008 Share Posted April 16, 2008 i have a simple form, and wanted to check if a check box is checked, if not, then dont go to next page, but if it is, then its ok. right now i have code to check text box values and see if they are filled in, but dont know how to check, to see if a check box is filled. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted April 16, 2008 Share Posted April 16, 2008 Like this: <?php if (isset($_POST['submit'])){ if (isset($_POST['check'])){ echo "Checked"; } else { echo "Not Checked"; } } ?> <form method="post"> <input type="checkbox" name="check" /> <input type="submit" name="submit" value="Submit" /> </form> Quote Link to comment Share on other sites More sharing options...
haku Posted April 16, 2008 Share Posted April 16, 2008 insert a check, submit the form, and echo the value. Then you'll know. (give a man a fish...) Quote Link to comment Share on other sites More sharing options...
jjmusicpro Posted April 16, 2008 Author Share Posted April 16, 2008 how do i do it with javascript, not on a post back? Quote Link to comment Share on other sites More sharing options...
micah1701 Posted April 16, 2008 Share Posted April 16, 2008 you move this thread to the javascript forum, not the php forum Quote Link to comment Share on other sites More sharing options...
craygo Posted April 16, 2008 Share Posted April 16, 2008 you need to name your form and have the name of the checkbox you want check. Here is an example <SCRIPT LANGUAGE="JavaScript"> function val_check(){ if(document.form1.checkbox1.checked == false){ alert("Please check off a box"); return false; } return true; } </script> <form name="form1" method="POST" action="" /> <input type="checkbox" name="checkbox1" value="1" /> <input type="submit" name="submit" value="submit" onClick="return val_check;" /> </form> Ray Quote Link to comment Share on other sites More sharing options...
haku Posted April 16, 2008 Share Posted April 16, 2008 if(document.form1.checkbox1.checked == false) This is old javascripting and doesn't follow the current DOM standards. It also cannot be done with valid XHTML as the 'name' attribute is deprecated for forms now. One way it can be done these days is like this: (x)html <input type="checkbox" id="target" name="checkbox1" value="1" /> Javascript if(document.getElementById('target').checked == false){ document.getElementById targeting 'id's is a pretty common and safe method. Quote Link to comment Share on other sites More sharing options...
benphp Posted April 16, 2008 Share Posted April 16, 2008 There's an excellent form validator here: http://www.javascript-coder.com/html-form/javascript-form-validation.phtml Quote Link to comment Share on other sites More sharing options...
haku Posted April 16, 2008 Share Posted April 16, 2008 That's a good little tool (potentially - I didn't look at it too closely). But it should only be used for extra functionality, not for the final check. If someone wants to bypass javascript validation they just need to turn off javascript. Quote Link to comment Share on other sites More sharing options...
benphp Posted April 16, 2008 Share Posted April 16, 2008 Right - js is good for alerts but not solid validation. I use this structure for all SQL and validation: FormPage > SqlPage > ResultPage The ResultPage and FormPage can be the same, but the SqlPage should be independent and have no HTML in it. There, you can put in all your validation and use header() to redirect, and send back an error message in a ?msg=Checkbox%20Not%20Checked and such. It also has the added benefit of killing the post, so when someone clicks the back button, they don't get the nasty "Retry Post" error. 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.