Jump to content

make sure a checkbox is checked.


jjmusicpro

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.