【问题标题】:jQuery Ajax Store Data to database by multiple checkbox checkjQuery Ajax 通过多个复选框检查将数据存储到数据库
【发布时间】:2020-08-28 18:23:54
【问题描述】:

我下面有多个复选框

演示可以在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(
  {

  })
});

有人知道怎么做吗?

【问题讨论】:

  • 检查这个链接,它解释了我如何将数据从 JS 传递到 PHP,然后再传递给 JS。在您的情况下,PHP 将发布到数据库。 stackoverflow.com/questions/42070073/ajax-and-php-debug希望对您有所帮助
  • 首先,在JS中创建一个变量,里面包含你想要的数据。然后,使用 Post 方法。然后,在 PHP 文件中获取该数据。然后你可以随心所欲地使用它
  • @tiagoperes 我看到了那个帖子,但我认为这与我的情况不同。多个复选框
  • 如果你能举个例子就更好了

标签: javascript php jquery mysql ajax


【解决方案1】:

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

客户端:

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: '".$dias."'
//Connect    
$result = mysqli_query($conn, $sql);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-18
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    相关资源
    最近更新 更多