我在下面有多个复选框
javascript - jQuery Ajax通过多个复选框将数据存储到数据库-LMLPHP
演示可以找到here
我们可以在上面的图片中看到,如果我选中了其中的一些复选框,然后单击Update,那么它将显示每个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];

因为您还想将内容写入数据库,所以需要连接到数据库。
执行正常的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);

07-25 21:02
查看更多