在正确的mysql行后阵列复选框值不 [英] checkbox value in a post array not in correct mysql row

查看:92
本文介绍了在正确的mysql行后阵列复选框值不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我试图一个输入表单字段转换成一个复选框。在输入框中我输入1或0这工作得很好,并存储在正确的MySQL数据库。我想输入域更改为复选框,以便检查= 1 选中= 0

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 .

当我改变输入一个复选框值时,它不能正常工作。该值存储在MySQL中错了行。可能有人帮助,请

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.\n", $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");
}
?>

直到我改变存入一个复选框,输入各项工作的罚款。我在做什么错。我有评论支付,你可以在code看到并增加了一个名为的名称隐藏输入=支付[] 和checkbox 名称输入=支付[]

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' ?>/>

更新时支付的[]发布不发布未选中的复选框值= 0
谢谢
乔恩

UPDATE When the paid[] post it does not post the unchecked checkbox value=0 thanks jon

通过帮助下@aimme解决

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' ?>/>

添加JavaScript来改变隐藏的价值

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>

感谢您的帮助!

推荐答案

其因复选框的空值不与形式张贴在所有。这使得数组不包含空值向上移动。使用一个隐藏的文本框,每个复选框与在文本框中没有值至少空将与提交表单张贴。

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.

下面是你如何能做到这一点与jQuery

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>

这篇关于在正确的mysql行后阵列复选框值不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆