2017-03-04 161 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