How to get the check box value using POST method in PHP ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthik baskar
    New Member
    • Sep 2010
    • 16

    How to get the check box value using POST method in PHP ?

    I have a check box for which a user can check many values . I need to get the value of the check box in a variable in PHP to insert it into the database . Please someone help me ..
  • MartinBaril

    #2
    a checkbox can't have many value, it's ON or OFF. In php you don't receive unchecked checkbox.
    exemple
    Code:
    Cat <input type="checkbox" name="pet[]" value="cat">
    Dog <input type="checkbox" name="pet[]" value="dog">
    Pig <input type="checkbox" name="pet[]" value="pig">
    you will get an array of checked box. for exemple, you check cat and pig, you'll get for print_r($pet);
    Code:
          Array
    (
        [0] => cat
        [1] => pig
    )

    Comment

    • Mohammad Shahid

      #3
      there are different ways to play with checkboxes using php, see below code

      Code:
      <?php
      
      echo $_REQUEST['a']."<br />";
      echo $_REQUEST['b']."<br />";
      
      echo print_r($_REQUEST['c'])."<br />";
      
      ?>
      Code:
      <form action="" method="post">
        <input type="checkbox" name="a" value="a">
        <input type="checkbox" name="b" value="b">
        <input type="submit" name="Submit" value="Submit">
      </form>
      or

      Code:
      <form action="" method="post">
        <input type="checkbox" name="c[]" value="c1">
        <input type="checkbox" name="c[]" value="c2">
        <input type="submit" name="Submit" value="Submit">
      </form>
      Last edited by Dormilich; Nov 3 '10, 06:37 AM. Reason: please use [CODE] [/CODE] tags when posting code

      Comment

      • karthik baskar
        New Member
        • Sep 2010
        • 16

        #4
        Thanks a lot :)

        Comment

        Working...