简体   繁体   English

在php中获取多个复选框的值

[英]getting the values of multiple checkboxes in php

I have a form with different data on a row being fetched from the DB with unique id. 我有一个表单,其中包含从具有唯一ID的DB中提取的行上的不同数据。 And each data has two checkbox with specific values. 每个数据都有两个具有特定值的复选框。

I need to get the unique id of the data and the values of the two checkboxes whether checked or not. 我需要获取数据的唯一ID和两个复选框的值,无论是否选中。

Example:

General Config            [] Read    [] Write
Manage Admin Account      [] Read    [] Write
Manage Adverts            [] Read    [] Write
Manage Articles           [] Read    [] Write
Manage Groups             [] Read    [] Write

if a user checks one or both checkboxes for each row, I expect to get an array with the unique id of row that has it checkbox being checked and also the values of which ever checkbox was checked. 如果用户检查每一行的一个或两个复选框,我希望得到一个具有唯一ID行的数组,该行具有复选框,并且还检查了复选框的值。

Here is my code: 这是我的代码:

<form action="test.php" method="post">

<table width="400" border="0">
<?php
@include "../inc/db.php"; 

$all_mod_qry = @mysql_query("select * from `adm_modules` order by module_name");
while($all_mod_row = @mysql_fetch_assoc($all_mod_qry))
{
    echo '<tr>
    <td width="45%">
<p>
<label><span>'.$all_mod_row['module_name'].'</span></label>
  <div class="clear"></div>
</p>
    </td>
    <td>
<p>
<label><input name="mod_id[][]" type="checkbox" value="'.$all_mod_row['id']['read'].'" /> <em>Read</em> </label>
  <div class="clear"></div>
</p>
    </td>
    <td>
<p>
<label><input name="mod_id[][]" type="checkbox" value="'.$all_mod_row['id']['write'].'" /> <em>Write</em> </label>
  <div class="clear"></div>
</p>
    </td>
  </tr>';   
}

?>
</table>


<p>
<input class="submit_btn" type="submit" name="new_subt" value="Create" />
</p>

</form>


<?php


if (isset($_POST['new_subt'])) {

foreach ($_POST['mod_id'] as $mod_id => $mod_right_arr)
{
    $module[$mod_id] = array();

    foreach ($mod_right_arr as $key => $value)
    {
        $module[$mod_id][$key] = $value;
    }
}


}


?>

Being trying to get this to work but it just would not. 试图让这个工作,但它不会。

Really need help with this and will be grateful to get help with this. 真的需要这方面的帮助,并将很高兴得到这方面的帮助。

Try to give checkbox names like 尝试给出复选框名称

<label><input name="mod_id[]" type="checkbox" value="'.$all_mod_row['id']['read'].'" /> <em>Read</em> </label>

with single dimentional...and try to get them like 单一维...并尝试让他们喜欢

$checks = $_POST['mod_id'];

And if possible try to give two array of names for read and write,it is much and much easier for you to choose and retrieve the selected data 如果可能的话,尝试为读取和写入提供两个名称数组,您可以更轻松地选择和检索所选数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM