使用开关盒(Ajax)不带提交按钮的PHP更新状态

使用开关盒(Ajax)不带提交按钮的PHP更新状态,php,ajax,Php,Ajax,我有一个注册用户信息列表。Web管理员将在需要时激活任何符合条件的用户。用户列表是带有复选框的表格形式。我想通过勾选复选框启用用户访问,而不提交web表单。如何做到这一点 <label class="switch"> <input type="checkbox" <?php if($row['status']=="1") {?> checked="true" <?php } ?>> <span class="slider round">

我有一个注册用户信息列表。Web管理员将在需要时激活任何符合条件的用户。用户列表是带有复选框的表格形式。我想通过勾选复选框启用用户访问,而不提交web表单。如何做到这一点

<label class="switch">
<input type="checkbox" <?php if($row['status']=="1") {?> checked="true" <?php } ?>>
<span class="slider round"></span>
</label> 

>

使用ajax请求。给复选框一个id

$('#id-of-checkbox').click(function(){
    var checked = $(this).prop('checked');
    $.post('/url-to-ajax-to', {checked: checked}, function(e){
        console.log(e); // do what you need to do afterwards in here
    });
});
您可能也想传递一个行ID。 然后在PHP scipt中创建一个SQL查询:

$sql = "UPDATE sometable SET checkboxcolumn = :checkval WHERE id = :id";
正常绑定参数,执行查询,然后返回json响应

header('Content-Type: application/json');
echo json_encode([
    'whatever' => 'data you might like to return',
    'status' => 'success' //etc
]);
exit;