首页 文章

使用ajax发布和更新复选框的值

提问于
浏览
0

基于我昨天的帖子how to update and post the value of checkbox from ajax call我把我的代码改为了这个

$query="select * from student";
$result=mysql_query($query)or die(mysql_error());
while($rs=mysql_fetch_array($result))
{
?>
<tr>
      <td align="center"><?php echo $rs['st_id']; ?></td>
<td align="center"><?php echo $rs['name']"; ?></td>
<td align="center"><input type="checkbox" name="checked" onclick="UpdateCheckBox()" <?php if($rs['checked']==1){echo "checked"; } ?> /></td>
<td align="center"><a href="delete_student.php?id="><img src="images/delete_icon.png" alt="Delete" /></a></td>
<td align="center"><a href="update_student.php?id="><img src="images/update.png" alt="Update" /></a></td>
</tr>
<script type="text/javascript" src="jquery.js">
    function UpdateCheckBox()
{
   var st_id = <?php echo $rs['st_id']; ?>;
    $('input[type=checkbox]').click(function(){
    var chkName = $(this).attr('name');
    var checkVal = $(':checkbox[name='+chkName+']').attr('checked');//true or false
    $.ajax({
      url: 'update.php?checboxName=' + checkVal,//Do update on server-side
      success: function(data) {
        alert('Updated successful.');
      }
    });
  });
}
    </script>
  <?php
  }

   ?>
    </tbody>
</table>

我的update.php代码是

$conn=new LoginSystem();
$conn->connect();
$update=$_GET['checboxName'];
$sql="UPDATE student SET checked='$update'";
$rs=mysql_query($sql);
?>

当我点击复选框时,没有任何事情发生,当刷新整个页面时,它会自动取消选中 . 请注意,没有表单或提交按钮,单击事件上的复选框上的所有操作都已完成 . 我想通过单击填充表中的复选框来更新数据库 . 请帮忙

1 回答

  • 0

    我们无法告诉你为什么 update.php isn 't working since you didn' t分享该代码 . 现在,它可以't possibly work, as you'没有提供任何方式来知道要更新的行以及是否已选中该框 .

    您需要为每行中的复选框指定不同的名称,以便您知道在PHP代码中检查了哪一个 . 在名称中使用行的标识符,如 checkbox_3checkbox[3] . 然后,您需要确保将复选框的值作为URL的一部分 .

相关问题