拆分 AJAX POST 个变量
Splitting AJAX POST variables
每当单击 checkbox
时,我都会为 post AJAX
变量编写脚本。 When multiple checkboxes
are selected the variables are stored in an array and seperated by a 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 );
我知道我需要以某种方式存储这些变量,然后执行 for each
来生成单独的查询,但我不确定该怎么做。
这样做:
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
}
}
}
使用这个,
function tblProperties() {
if (isset ( $_POST ['tbl'] )) {
$tbl = $_POST ['tbl'];
$db = $_POST ['db'];
$link = mysqli_connect ( 'localhost', 'root', '', $db );
$tblarr = explode('|', $tbl);
for($i = 0 ; $i < sizeof($tblarr); $i++)
{
$qry = "DESCRIBE $tblarr[$i]";
$result[] = mysqli_query ( $link, $qry );
}
每当单击 checkbox
时,我都会为 post AJAX
变量编写脚本。 When multiple checkboxes
are selected the variables are stored in an array and seperated by a 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 );
我知道我需要以某种方式存储这些变量,然后执行 for each
来生成单独的查询,但我不确定该怎么做。
这样做:
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
}
}
}
使用这个,
function tblProperties() {
if (isset ( $_POST ['tbl'] )) {
$tbl = $_POST ['tbl'];
$db = $_POST ['db'];
$link = mysqli_connect ( 'localhost', 'root', '', $db );
$tblarr = explode('|', $tbl);
for($i = 0 ; $i < sizeof($tblarr); $i++)
{
$qry = "DESCRIBE $tblarr[$i]";
$result[] = mysqli_query ( $link, $qry );
}