使用ajax发布和更新复选框的值
基于我昨天的帖子如何更新和发布从ajax调用复选框的值我改变了我的代码,这个ie
$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); ?>
当我点击复选框时,什么也没有发生,当刷新整个页面时,它会自动取消选中。 请注意,没有任何表单或提交按钮,所有事情都在点击事件上的复选框上完成。 我想通过点击填充表中的复选框来更新数据库。 请任何帮助
我们不能告诉你为什么update.php不工作,因为你没有共享该代码。 现在,它不可能工作,因为你没有提供任何方式来知道要更新哪一行以及该盒子是否被选中。
您需要在每行中给出不同名称的复选框,以便您知道在PHP代码中检查了哪一个。 在名称中使用行的标识符,如checkbox_3或checkbox[3] 。 然后,您需要确保您也使URL的复选框的值部分。
上一篇: post and update value of checkbox using ajax
下一篇: how to update and post the value of checkbox from ajax call
