简体   繁体   中英

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. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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