开发者

How to receive a batch of checkbox data in PHP?

开发者 https://www.devze.com 2022-12-11 06:47 出处:网络
The key code is: while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) { $id=$row[id]; $html=<<<html

The key code is:

  while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{ $id=$row[id];
  $html=<<<html
<tr><td> 
<input style="float:left" type="checkbox" name="mycheckbox[]"  value="$id">   
<span style="float:left">$row[content]</span>
<span style="col开发者_如何学Goor:black;float:right">$row[submitter]</span></td></tr>  
html;
echo $html; 
}

There are many checkboxes.

How to receive these checkbox values in another PHP file?

You know, you need to name these checkboxes so a PHP file can receive these values on the other side. But how to name these checkboxes? If I name 1,2, 3,...,how can I associate them with $row[id]?


You need to give them names - you can either do it like this:

<input style="float:left" type="checkbox" id="$id" name="$id" value="true">

or like this to get an array:

<input style="float:left" type="checkbox" id="$id" name="myBoxes[$id]" value="true">

You can then check isset($_POST[$id]) or isset($_POST['myBoxes'][$id])


Name your checkboxes so you can easily identify them. eg

 $CheckBoxHTML =  "<input type='checkbox' name='check_$row[id]' value='YES'>Check This";

then in your recieving php file you can find all checkboxes by

foreach ($_POST as $key => $value)
{
  if (strpos($key,'check_') !== false)
  {
     list($tmp, $ID) = split('_', $key);

      $CheckedValues[] = $ID;

   } 

}

That will pull out all your checked ids.

0

精彩评论

暂无评论...
验证码 换一张
取 消