dsd30433 2015-09-05 10:47
浏览 44
已采纳

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!

  • 写回答

1条回答 默认 最新

  • dongsi2317 2015-09-05 11:39
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况