Naveenbos
Naveenbos

Reputation: 2562

How do I get the value of a HTML checkbox input element?

    <?php

    include ("database.php");               
    $qry = "Select * from tbnam where $option like '%$content%'";
    $result=mysql_query($qry);        
    while ($row =mysql_fetch_array($result))
    {
    $id=$row['id'];
    ?>
//creating table
    <tr><?echo "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\"  value=\"".$row['id']."\" /></td>";?>
    <td><? echo $row['name'];?></td>
    <td><? echo $row['address'];?></td>
    <td><? echo $row['email'];?></td>
    <td><? echo $row['telephone'];?></td>
    <td><? echo $row['problem'];?></td>
    <td><? echo $row['reply_query'];?></td>
    <td><? echo $row['type'];?></td>
    <td><? echo $row['other'];?></td></tr>
    <?php
    }
    ?>
    <input type="submit" name="search" value="Print" size="10"/>
    </form>
    </table>

how i got the checkbox values? any need of passing $id in checkbox[] array ? if yes how it is possible? help me...name=\"checkbox["\".$id"\"]\"

Upvotes: 2

Views: 734

Answers (3)

unni
unni

Reputation: 2991

By writing

<tr><?echo "<td><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\"  value=\"".$row['id']."\" /></td>";?>

you have designed a form which returns an array of values with each value corresponding to a checkbox that has been "checked".

In this example the POST variable that points to this array will be $_POST['checkbox']. Since you have set the value of each checkbox as $row['id'], each element in the checkbox array will have the 'id' value corresponding to each checkbox that you have checked.

You can check out these values like this:

foreach($_POST['checkbox'] as $value)
{ 
  echo $value;
}

or simply by saying:

echo var_dump($_POST['checkbox']);

Hope this makes it clear. :)

Upvotes: 2

AjayR
AjayR

Reputation: 4179

What is the need of the array here (checkbox[]) , when you are showing a single checkbox element here. Also for IDs, we need not to mention as arrays.

You simply use like below

<tr><?echo "<td><input type=\"checkbox\" name=\"checkbox\" id=\"checkbox\"  value=\"".$row['id']."\" /></td>";?>

And While reading,

if (isset($_POST["checkbox"])) // Returns true if checked
{

}

Hope it helps.

Upvotes: 0

xdazz
xdazz

Reputation: 160833

$_POST['checkbox'] will give you an array of the value you checked.

Upvotes: 1

Related Questions