I have written a script to post AJAX
variables whenever a checkbox
is clicked. When multiple checkboxes
are selected the variables are stored in an array and seperated by a pipe
. I need each variable to be passed separately into my php
function so I can run separate queries.
My 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
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.
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 );
}