gpt4 book ai didi

javascript - jQuery Ajax 通过多个复选框检查将数据存储到数据库

转载 作者:行者123 更新时间:2023-11-29 03:22:05 25 4
gpt4 key购买 nike

下面有多个复选框

enter image description here

可以找到Demo here .

我们可以在上图中看到,如果我选中该复选框的某些部分,然后单击“更新”,它将显示每个 ID 的复选框值。

现在我想将它存储到数据库中。这是我的表格:

ID | CHKSTATUS
1 | update
2 | create
2 | delete

我需要使用 jQuery Ajax 将其存储到 PHP

$(function(){
$('#btnUpdate').click(function(){
var cb = [];
$.each($('input[type=checkbox]:checked'), function(){
cb.push($(this).data('id') + ' -> ' +$(this).data('tipe'));
});
$('#status').val(cb.join("\n"));
});

$.ajax(
{

})
});

有人知道怎么做吗?

最佳答案

您需要的过程可以分解为以下步骤:

客户端:

var mydate = (the data you want to pass to the PHP file); // create a variable with the data you need
$.ajax({ //start an ajax call
type: "POST", //post method
url: 'script.php', //define which php file you want to post to
data: ({dates: mydate}), //define which data you want to pass to
dataType : 'html', //the type of the data
success: function(data) { //what to do after being posted
alert(data);
}
});

服务器端:然后,在 PHP 文件 (script.php) 中,这就是您获取数据的方式:

$dias= $_POST[dates];

因为还要往DB里写东西,所以需要连接DB。进行正常的 PHP 连接:

// variables
$servername = "server";
$username = "username";
$password = "password";
$dbname = "database";
//Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//Make database query
$sql = "***"; // this is how you're going to use the variable in the query: '".$dias."'
//Connect
$result = mysqli_query($conn, $sql);

关于javascript - jQuery Ajax 通过多个复选框检查将数据存储到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42593980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com