簡體   English   中英

jQuery Ajax 通過多個復選框將數據存儲到數據庫

[英]jQuery Ajax Store Data to database by multiple checkbox check

我在下面有多個復選框

在此處輸入圖片說明

演示可以在這里找到。

我們可以在上圖中看到,如果我選中其中的一些復選框,然后單擊“更新”,它將顯示每個 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM