haruya
haruya

Reputation: 63

php : my foreach loop checkbox did not send value properly

My problem with the checkbox is , it didn't send the value as i want..

This is my html code for the checkbox . I use foreach loop.

<form id="signupform" autocomplete="off" method="post" action="inputchecklist.php" class="form_container left_label">
<?php 
$ew = mysql_query("select * from pos_unit where pos_unit_name like '%Director%'");
$row = mysql_num_rows($ew);
while($eq = mysql_fetch_array($ew))
{
$pos = $eq['pos_unit_name'];
$checklist = mysql_query("select * from checklist where check_pos = '$pos' and check_ok = 'Check'");
$excheck = mysql_fetch_array($checklist);
$poss = $excheck['check_pos'];
?>
<li>
<div class="form_grid_12">
<label class="field_title" ><?php echo $eq['pos_unit_name']; ?></label>
<div class="form_input">
<input name="check[]" class="checkbox" type="checkbox"  value="<?php echo $eq['pos_unit_name']; ?>" style="opacity: 0;">
</div>
</div>
</li>
<?php } ?>
</form>

and this is the php code for insert..

<?php 

include 'config.php';

$code = $_POST['code'];

$targ = $_POST['selectdistarg'];

$check = $_POST['check'];
$pos = $_POST['pos'];


foreach($pos as $index => $names)
{
    $insert = mysql_query("insert into checklist (check_ok,check_pos,check_code) values ('$check[$index]','$names','$code')") or die (mysql_error());
}

$update = mysql_query("update corporate_kpi set kpi_dist_targ = '$targ' where kpi_code = '$code'");

?>

For example,

Checkbox 1 - A, Checkbox 2 - B, Checkbox 3 - C

If we choose number 2, value is B, but my code is, if we choose number 2, the value is A and if we choose number 3, the value is also A, i don't know why..

This is the <th> that generate based on db

<?php 
while ($eq = mysql_fetch_array($ew))
{
$pos = $eq['emp_data_pos'];
?>
<th><?php echo $eq['emp_data_pos']; ?></th>
<?php } ?>

and this is the <td> value..

<?php $qq = mysql_query("select emp_data_pos from emp_data where emp_data_pos like '%Director%'");
while ($ee = mysql_fetch_array($qq))
{
$pos = $ee['emp_data_pos'];
$pos1 = mysql_real_escape_string($pos);
$aa = mysql_query("select * from checklist where check_pos = '$pos1' and check_code = '$code' and check_ok = 'Check'");
$bb = mysql_fetch_array($aa);
$ok = $bb['check_ok'];
if($ok != "")
{
$ok = '&#10004;';
} else {
$ok = '';
}
?>
<td class="center">
<?php echo $ok; ?>
</td>
<?php } ?>

Upvotes: 0

Views: 785

Answers (1)

Evan
Evan

Reputation: 123

You should really populate your checkboxes with the values you want to send. So your checkboxes should get the values A, B, C etc.. So your checkboxes should look like this:

<input name="check[]" class="checkbox" type="checkbox" value="<?php echo $eq['pos_unit_name']; ?>" style="opacity: 0;">

That way, your check-array will only be populated by those values you selected.

If I recall correctly, unchecked checkbox will not be sent at all. So if you just checked Checkbox B, your array will consist of only one value. That way, you lose the connection to your pos-Array.

Upvotes: 1

Related Questions