我有多个复选框
可以找到演示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(
{
})
});
任何人都知道如何做到这一点?
答案 0 :(得分:0)
您需要的流程可分为以下几个步骤:
客户端:
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];
因为您还想将内容写入数据库,所以需要连接到数据库。 做正常的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: '".$data."'
//Connect
$result = mysqli_query($conn, $sql);