2017-03-04 71 views
2

數據庫中,我有以下jQuery的Ajax的數據存儲到多個複選框檢查

enter image description here

演示多個複選框可以發現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

檢查這個環節,它的解釋我是如何從JS數據再次傳遞給PHP,然後給JS。 就你而言,PHP會發布到數據庫。 http://stackoverflow.com/questions/42070073/ajax-and-php-debug 希望這有助於 – brotherperes

+0

首先,在JS中用你想要的數據創建一個變量。然後,使用Post方法。然後,抓住PHP文件中的數據。那麼你可以用它來做任何你想要的東西 – brotherperes

+0

@tiagoperes我看到那篇文章,但我認爲它與我的情況有所不同。多個複選框 –

回答

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); 
+0

這對你有幫助嗎? – brotherperes

相關問題