Warm tip: This article is reproduced from serverfault.com, please click

php-通过使用Ajax单击不带提交按钮的复选框来更新sql数据库

(php - Update sql database by clicking checkbox without submit button using ajax)

发布于 2020-11-27 23:47:04

我正在寻找一种方法来更新我的sql数据库,而无需通过选择复选框来刷新页面。我正在尝试实现当用户选择复选框时要传递给dolead.php的ID,然后检查ID是否高于0,它必须传递值1否则为0。使用where子句中发布的ID进行更新。在我的代码下面,我在做什么错?我的日志中没有错误,并且错误日志记录如下: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检索发布值并更新数据库;

$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:包含连接文件,但此处未显示。

Questioner
newtocss
Viewed
11
KUMAR 2020-11-28 09:38:19

你可以将a添加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