我编写了一个脚本来在单击复选框
时发布AJAX
变量。当选择多个 checkboxes
时,变量存储在一个数组中并由 pipe
分隔。我需要将每个变量单独传递到我的 php
函数中,以便我可以运行单独的查询。
我的 JavaScript
$(document).on("change", ".tbl_list", function () {
var tbls = new Array();
$("input:checkbox[name='tbl[]']:checked").each(function () {
tbls.push($(this).val());
});
var tbl = tbls.join('|');
var db = window.sessionStorage.getItem("db");
alert(db);
$.ajax({
type: "POST",
url: "ajax2.php",
data: {
tbl: tbl,
db: db
},
success: function (html) {
console.log(html);
$("#tblField").html(html).show();
}
});
});
选中多个表时的输出
tbl:db|event
我的 PHP 函数
function tblProperties() {
if (isset ( $_POST ['tbl'] )) {
$tbl = $_POST ['tbl'];
$db = $_POST ['db'];
$link = mysqli_connect ( 'localhost', 'root', '', $db );
$qry = "DESCRIBE $tbl";
$result = mysqli_query ( $link, $qry );
我知道我需要以某种方式存储这些变量,然后为每个变量执行一个 以生成单独的查询,但我不确定该怎么做。
最佳答案
这样做:
function tblProperties() {
if (isset ( $_POST ['tbl'] )) {
$db = $_POST ['db'];
$tables = explode('|', $_POST ['tbl']); // explode your variable
$link = mysqli_connect ( 'localhost', 'root', '', $db );
foreach($tables as $table) // foreach through it
{
$result = mysqli_query ( $link, "DESCRIBE $table" ); // do your query
while($row = mysqli_fetch_array($result))
{
// your code here
}
}
}
关于javascript - 拆分 AJAX POST 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29341069/