0

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 } ?>
3
  • what is the value of <?php echo $eq['pos_unit_name']; ?> ? , is it dynamic? in hidden field, probably you need to update the values based on radio selection Commented Apr 23, 2014 at 8:52
  • in your html code.. input checkbox tag generated dynamically but they has same value attribute "Check" Commented Apr 23, 2014 at 8:57
  • yes it has same value , do i have to differentiate it? Commented Apr 23, 2014 at 9:01

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it is, if i check the 2nd array, the value are inserted in the 1st array, how do i make the only i checked is the only value that is send..
I edited my answer to include code, so that your checkboxes will transfer the value, not the hidden inputs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.