post数组中的checkbox值不在正确的mysql行中
Hi i am trying to convert a input form field into a checkbox. in the input field i enter either 1 or 0 this works fine and stores correctly in MySQL database. I am trying to change the input field to a checkbox so checked = 1
and unchecked =0
.
When i change the input into a checkbox value it does not work correctly. The values get stored in the wrong rows in mysql. Could someone help please
<?php include('config.php'); ?>
<?php $mm = "SELECT * FROM test_mysql";
$result = mysqli_query($mysqli, $mm) or die('-1'.mysqli_error());
$count = $result->num_rows;
printf("Result set has %d rows.
", $count);
// Count table rows
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form method="post" action="">
<tr>
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Paid</strong></td>
</tr>
<?php
while ($rows = mysqli_fetch_assoc($result)) {
$checked = $rows['paid'];
?>
<tr>
<td align="center">
<input name="id[]" type="text" id="id" value="<? echo $rows['id']; ?>">
</td>
<td align="center">
<input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname[]" type="text" id="lastname" value="<? echo $rows['lastname']; ?>">
</td>
<td align="center">
<?php /*?><input name="paid[]" type="text" id="paid" value="<? echo $rows['paid']; ?>"><?php */?>
</td>
<td align="center">
<input type="hidden" name="paid[]" value="0" />
<input type="checkbox" name="paid[]" value="1"<?php if ($checked == 1 ) echo 'checked' ?>/>
</td>
</tr>
<?php } ?>
<tr>
<td colspan="4" align="center"> <button type="input" name="submit" value="editTask" class="btn btn-success btn-lg btn-icon"><i class="fa fa-check-square-o"></i>submit</button></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if (isset($_POST['submit']) && $_POST['submit'] == 'editTask'){
$entryId = $mysqli->real_escape_string($_POST['id']);
foreach($_POST['id'] as $key=>$id) {
$name = $_POST['name'][$key];
$lastname = $_POST['lastname'][$key];
$paid = $_POST['paid'][$key];
$stmt = $mysqli->prepare("UPDATE test_mysql SET name=?, lastname=?, paid=? WHERE id = ?");
$stmt->bind_param('ssss', $name, $lastname, $paid,$id);
$stmt->execute();
}
$stmt->close();
header("location:page.php");
}
?>
Every work fine until i change the input paid into a checkbox. What am I doing wrong. I have commented the input paid out as you can see in the code and added a hidden input called name=paid[]
and checkbox name=paid[]
<input type="hidden" name="paid[]" value="0" />
<input type="checkbox" name="paid[]" value="1" <?php if ($checked == 1 ) echo 'checked' ?>/>
UPDATE When the paid[] post it does not post the unchecked checkbox value=0 thanks jon
SOLVED WITH HELP FROM @aimme
<input type="hidden" class="checkbox_handler" name="paid[]" value="<? echo $rows['paid']; ?>"<?php if ($checked == 1 ) echo 'checked' ?> />
<input type="checkbox" name="paidcheck[]" value="1" <?php if ($checked == 1 ) echo 'checked' ?>/>
add JavaScript to change hidden value
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(document).on("change", "input[type='checkbox']", function() {
var checkbox_val = ( this.checked ) ? 1 : 0;
$(this).siblings('input.checkbox_handler').val(checkbox_val);
});</script>
thanks for your help!
Its because the null values of check boxes are not posted with the form at all. This makes array to shift upward without including the null values. Use a hidden text box with each check-box as with no values in the text box at least the null will be posted with the form on submit.
Here is how you could achieve this with Jquery
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
$checked=0;
?>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<form method="post" action="">
<input type="hidden" name="paidHidden" id="paid" value="<?php echo ($checked == 1)?'1':'0'; ?>" />
<!-- for debugging to check whether it works
<input type="text" name="paidHidden" id="paid" value="0" />
-->
<input type="checkbox" name="dummy" id="dummy" <?php echo ($checked == 1)?'checked':''; ?>/>
<input type="submit" name="sub" />
</form>
<script>
$("#dummy").click(function(){
if ($(this).is(":checked"))
{$("#paid").val(1);}
else
{$("#paid").val(0);}
});
</script>
</body>
</html>