How do I know whether a checkbox is checked or not in PHP?

Discussion in 'PHP' started by Imozeb, Mar 11, 2010.

  1. #1
    I have a form that is submited to PHP, PHP evaluates it and sends it to my database. How do I get the value of a checkbox as 0 or 1? I used import_request_variables to get the data and it always returns 0.
     
    Imozeb, Mar 11, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Heres an example:

    Your html form should contain the following...

    <input type= 'checkbox' name='test' value="0">Test 0

    <input type= 'checkbox' name='test' value="1">Test 1

    Your php...

    <?php
    
    //will display the checked value (0 or 1)...
    echo $_POST['test'];
    
    //check if value is 0...
    if($_POST['test'] == 0){
    //its 0 thats selected...
    
    }
    ?>
    PHP:
     
    danx10, Mar 11, 2010 IP