How to check if checkbox are selected or not

Hi,
i was making a form and wondering. when u have a check box. how to check if its checked or not before sending it value to database.? and in database its type will be a tiny int.right?

if a checkbox is checked it’s value will be “on”, if it’s not set there is no value in $_POST.

To make this behavior a little more consistent and predictable, and to make sure you always get some value regardless whether the checkbox was checked or not I always use something like


<input type="hidden" name="somecheckbox" value="0" />
<input type="checkbox" name="somcheckbox" value="1" />

When the checkbox is checked the value of the <input type=“checkbox” … /> is send with the post data, and if it isn’t checked the value of the <input type=“hidden” … /> is send with the post data, so you can just check whether the value is 0 (unchecked) or 1 (checked).

And indeed, in the database it customary to use a tinyint to store boolean values :slight_smile:

Ah, that’s clever and explains. however how to i insert this in database? i mean how do sent the checkbox which is selected into database as it got 1
p.s - i am new at this

Are you confusing checkboxes and radioboxes? It sounds like you are


<input type="hidden" name="extratopping" value="0" /> 
<input type="checkbox" name="extratopping" value="1" /> I would like extra pizza toppings


[ ] I would like extra pizza topings


What pizza topping would you like?
<input type="radio" name="topping" value="salami" /> Salami
<input type="radio" name="topping" value="pepper" /> Pepper
<input type="radio" name="topping" value="anchovies" /> Anchovies


What pizza topping would you like?
( ) Salami
( ) Pepper
( ) Anchovies

No am not confusing radio with checkbox. i mean suppose from your example above a person select one option (he checks it). how would i write a query to send it into a database and if haven’t selected it alert him (if mandatory) or else if he skips it. what will go into database?

if he checks it you put a 1 in the database, if he skips it you put a 0 in the database.
How to show an error is a whole other story, but the basic outline is something like (pseudo code):


if (form submitted) {
  if (!checkbox checked) {
    add error "You need to check the checkbox!"
  }
}
<html>
  ....
  {if errors} show errors {/if}
  <form> ... </form>
  ...
</html>

What an interesting technique, never thought about doing something like that!

here the best way i think atleast

$_post['checkbox']=$checkbox;
$file="text.txt";
$saved_file = fopen($file, 'r+');
if (!empty($checkbox)){
fputs(($saved_file, $checkbox);
fclose($saved_file);
}
else{
print "blah blah";
}

remeber every check box has a hidden value and ehn you check that box and submit it in relaity you are submitting that value which could be anything, this shows how you would collect that data and then write it to a txt file
that my way atleat, hope it helps

It seems awfully hacky to me.

Is this not dependant on how the browser serialises the form data to send to the server? What would happen in IE10 decides to send 1 before 0, would this therefore default to unchecked?

In addition to this, adding an extra element (the html input) to every checkbox just to deal with something that, IMO, is the responsibility of the application to enforce seems wrong.


<?php
$isChecked = array_key_exists('checkbox', $_POST);

I believe your a expert in programming and i am just a starter and it just slipped over my head and i didn’t got anything

Hi nofel,

I’m sorry you were left confused, my response was aimed at Immerse and ScallioXTX.

To go back to your original question though, try this form:-


<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
  echo '<pre>', print_r($_POST, true); exit;
}
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <form action="#" method="post">

      <h4>Toppings?</h4>

      <label for="topping-cheese">Cheese</label>
      <input id="topping-cheese" type="checkbox" name="toppings[]" value="cheese" />

      <label for="topping-ham">Ham</label>
      <input id="topping-ham" type="checkbox" name="toppings[]" value="ham" />

      <label for="topping-mushroom">Mushroom</label>
      <input id="topping-mushroom" type="checkbox" name="toppings[]" value="mushroom" />

      <input type="submit" value="submit" />

    </form>
  </body>
</html>

As described in your other thread, have a little play around with it. When you submit the form, you will see what data PHP receives; we can then build on that. :slight_smile:

I’ve been using this method for a very long time and never had any problem with it, and I’m certainly not the only one to use it, so I’m pretty confident it will keep working in the future because it would break too many websites if it didn’t work anymore. Of course, one can never know for sure (especially with the whimsy MS in the mix …)

Off Topic:

Must. Resist. Replying. :stuck_out_tongue:

Off Topic:

i know, u must be laughing on my code (:

but i am really new at this, i just need to make a form on which there are few checkboxes.i need there values to be emailed. and i need it by today :frowning:

Again, that was at ScallioXTX. :wink: