【问题标题】:Update sql database by clicking checkbox without submit button using ajax通过单击复选框而不使用 ajax 提交按钮来更新 sql 数据库
【发布时间】:2020-11-27 23:47:04
【问题描述】:

我正在寻找一种在不刷新页面的情况下通过选中复选框来更新我的 sql 数据库的方法。 当用户选择复选框时,我试图实现要传递给 dolead.php 的 id,而不是检查 id 是否高于 0,它必须传递值 1,否则传递值 0。检查后我需要数据库使用发布的 id 的 where 子句进行更新。在我的代码下面,我做错了什么?我的日志中没有错误,并且错误日志如下所示:mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

用户看到的 PHP 选择开关框来触发选择和 jquery 来触发帖子。

<head>

<script src="jquery-3.2.1.min.js"></script>
<link rel="stylesheet" href="switch.css" type="text/css">


</head>

<body>

    <table id="myTable" align='left' class="deftable">
    <thead>
    <tr>
    <th style="width:10px !important;">!!</th>
    <th>Name</th>
    <th>Notes</th>
    <th>Category</th>
    </tr>
    </thead>
    <tbody>
    
    <?  //Selecteer accounts.
    
    $stmt = $mysqli->prepare("SELECT * FROM account WHERE purpose = 'Crew4' AND level <= ? AND betaald = 'yes' 
    AND group_name = ? AND secret = 'no' ");
    $stmt->bind_param("is", $status,$groupname);
    $stmt->execute();
    $result = $stmt->get_result(); //only works when nd_mysli is set on the server!

    while ($row = $result->fetch_assoc()) {
  ?>  
    <tr>
       <td class="footer"> 
  <label class="switch">
  <input type="checkbox" <?php if($row['do_lead']=='1')  {
                 echo 'checked="checked"';
 } ?> value="<?php echo filter_var($row['id'], FILTER_VALIDATE_INT); ?>" />
  <div class="slider round">
    <span class="on">ON</span>
    <span class="off">OFF</span>
  </div>
</label></td>
    <td><?= htmlspecialchars($row['name']) ?></td>
    <td><?= htmlspecialchars($row['notes']) ?></td>
    <td><?= htmlspecialchars($row['purpose']) ?></td>
 
    
   

    </tr>
    <? } ?>
    </table>


</body>

<script>

$(document).ready(function()

{

var x=$("input[type=checkbox]").length;

$('input[type=checkbox]').on('change', function(i)

{

if($(this).is(':checked'))

{

x-=1;

if(x==0)

{

var val = [];

$(':checkbox:checked').each(function(i)

{

val[i] = $(this).val();

});

var jsonString = JSON.stringify(val);

$.ajax(

{

type: "POST",

url: "dolead.php",

data: {checkbox_value:jsonString},

cache: false,

success: function(data)

{

/* alert if post is success */

}

});

}

}

else

{

x+=1;

}

});

});

</script>

</html>

dolead.php 用于检索 post 值并更新数据库;

$data = json_decode(stripslashes($_POST['checkbox_value']));

foreach($data as $d){

    if($d > 0){$leadupdate = 1;}
    if($d == ''){$leadupdate = 0;}
        
    $stmt48 = $mysqli->prepare("UPDATE account SET do_lead = ? WHERE id = ? ");
    $stmt48->bind_param("ii", $leadupdate,$d);
    $stmt48->execute();
    $stmt48->close();       

}


?>

PS:连接文件已包含,但此处未显示..

【问题讨论】:

  • JS 错误怎么办?你检查过开发者工具栏控制台吗?
  • 是的。没有显示错误。
  • 网络标签呢?您确定 AJAX 调用发生了吗?你会得到什么回应?
  • 嗯。当我选择这些框时,我没有看到任何活动..
  • 看起来通话没有发生..脚本有什么问题..??

标签: php mysql ajax


【解决方案1】:

您可以在输入中添加class 并动态设置value:-

查看:-

<input class="active" type="checkbox" value="$row['do_lead']" >

jQuery/AJAX 调用:-

    <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<script type="text/javascript">
$(document).ready(function(e){ 
    $("input.active").click(function() {
    // store the values from the form checkbox, then send via ajax below
    var check_active = $(this).is(':checked') ? 1 : 0;
    var check_id = $(this).attr('value');
    
        $.ajax({
            type: "POST",
            url: "dolead.php",
            data: {id: check_id, active: check_active}
            success: function(response){
                alert('Data Updated Successfully');
    
            }
        });
    return true;
    });
});
</script>

PHP:-

// CLIENT INFORMATION
$active = mysqli_real_escape_string($_POST['active']);
$id = mysqli_real_escape_string($_POST['id']);

//update query.
//execute query.

注意:-更多关于click()的信息

https://api.jquery.com/click

【讨论】:

  • 感谢您的反应!尝试上述解决方案时,我的控制台日志中出现以下错误。未捕获的 SyntaxError:意外的标识符。似乎成功代码有问题。 ` data: {id: check_id, active: check_active} 成功: function(response){ alert('数据更新成功'); }`
  • 更新。有一个,失踪。代码执行但没有传递任何值?也许是因为表格生成了多个具有相同类的复选框?我没有收到任何错误,只是空值。
  • @newtocss 你能在 chechkbox 点击时获得check_activecheck_id 吗??
  • 对不起,我不知道你在问什么。由于帖子是空的,我猜是nog?在控制台中,我可以看到行 id 已填充,并且我看到您提供的脚本正在发送 1 或 0 值,如果我理解正确,因为我是 ajax 的新手..
  • 嗨库马尔。非常感谢您的解决方案在进行了一些更改后起作用。由于真正的转义字符串,这些值看起来是空的。当我删除那些更新发生和传递的值!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多