[英]Splitting AJAX POST variables
I have written a script to post AJAX
variables whenever a checkbox
is clicked. 只要单击一个
checkbox
我就编写了一个脚本来发布AJAX
变量。 When multiple checkboxes
are selected the variables are stored in an array and seperated by a pipe
. 选择多个
checkboxes
,变量存储在一个数组中并由pipe
分隔。 I need each variable to be passed separately into my php
function so I can run separate queries. 我需要将每个变量分别传递到我的
php
函数中,这样我就可以运行单独的查询。
My JavaScript 我的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();
}
});
});
Output when multiple tables are selected 选择多个表时的输出
tbl:db|event
My PHP function 我的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 );
I know I would need to store these variables somehow and then do a for each
to generate separate queries but i'm not sure how to do it. 我知道我需要以某种方式存储这些变量,然后
for each
变量生成一个单独的查询,但我不知道该怎么做。
Do this: 做这个:
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
}
}
}
use this, 用这个,
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 );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.