user3610827
user3610827

Reputation: 33

How to use GET to retrieve check box selected value in php

I am using Mysql PDO query to fetch the required result and it is saved in and array $newfamily. Now with the help of this array I am implementing check-box with the given code-

<form method="get" action="specific_family.php">
  <?php
    foreach($newfamily as $name)
            {
  ?>
        <input type='checkbox'<?php echo $name;?>"><?php echo $name;?></input>
 <?php
            } 
 ?>
 </select> <input type = "submit" name="submit" >
 </form>

Now in specific_family.php how can I retrieve al the selected check-box values in an array ? Also when I use back button of browser I can see previously selected values as ticked. How can I remove this ?

Please guide.

Upvotes: 0

Views: 90

Answers (2)

Quentin
Quentin

Reputation: 943579

The checkbox should have:

  • A label

The checkbox needs:

  • A name attribute
  • A value attribute

It must not have:

  • An end tag (unless you are using XHTML)

So:

<label>
<input type="checkbox"
      name="new_families[]"
      value="<?php echo htmlspecialchars($name); ?>">
<?php echo htmlspecialchars($name); ?>
</label>

The values of all the checked checkboxes will then appear in the array $_GET['new_families'] when the form is submitted.

Upvotes: 3

IrishGeek82
IrishGeek82

Reputation: 355

If you add the name attribute to your input thus:

 <form method="get" action="specific_family.php">
  <?php
    foreach($newfamily as $name)
            {
  ?>
                <label for="<?php echo $name?>"><?php echo $name?></label>
                <input type='checkbox' name="<?php echo $name;?>" id="<?php echo $name;?>"></input>
 <?php
            } 
 ?>
 </select> <input type = "submit" name="submit" >
 </form>

then your checkboxes will show up by name in your $_GET array.

Hope that helps :)

Upvotes: 0

Related Questions