SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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?
    All those who wander aren't lost.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    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

    HTML Code:
    <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
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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
    All those who wander aren't lost.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Are you confusing checkboxes and radioboxes? It sounds like you are

    HTML Code:
    <input type="hidden" name="extratopping" value="0" /> 
    <input type="checkbox" name="extratopping" value="1" /> I would like extra pizza toppings
    Code:
    [ ] I would like extra pizza topings
    HTML Code:
    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
    Code:
    What pizza topping would you like?
    ( ) Salami
    ( ) Pepper
    ( ) Anchovies
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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?
    All those who wander aren't lost.

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    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):

    Code:
    if (form submitted) {
      if (!checkbox checked) {
        add error "You need to check the checkbox!"
      }
    }
    <html>
      ....
      {if errors} show errors {/if}
      <form> ... </form>
      ...
    </html>
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    HTML Code:
    <input type="hidden" name="somecheckbox" value="0" />
    <input type="checkbox" name="somcheckbox" value="1" />
    What an interesting technique, never thought about doing something like that!

  8. #8
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here the best way i think atleast
    PHP Code:
    $_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
    PHP is FUN

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Immerse View Post
    What an interesting technique, never thought about doing something like that!
    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 Code:
    <?php
    $isChecked 
    array_key_exists('checkbox'$_POST);
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    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 Code:
    <?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
    All those who wander aren't lost.

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?php
    if('POST' === $_SERVER['REQUEST_METHOD']){
      echo 
    '<pre>'print_r($_POSTtrue); 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.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    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?
    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 ...)
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  13. #13
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Must. Resist. Replying.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  14. #14
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Off Topic:

    Must. Resist. Replying.
    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
    All those who wander aren't lost.

  15. #15
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Again, that was at ScallioXTX.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •