I have the below jquery that uses POST method and returns HTML data (checkboxes) from ajax.php file and works well.
Could you please advise how could I change the method to .getJSON so I can retrive multiple arrays from PHP? I'm quite new to the AJAX topic and already tried many ways but nothing has worked for me.
//jquery
$.post('ajax.php', {ZemeArray,MestoArray},function(data) {
$(".CHCB:eq(0)").html(data);
//ajax.php function that returns block of html checkboxes
DisplayFilteredItems("Zeme","Zeme");
I already tried to change it as follows, but the PHP function that returns HTML code is wrong:
//jquery
$.getJSON('ajax.php', {ZemeArray,MestoArray},function(data) {
$(".CHCB:eq(0)").html(data.a);
$(".CHCB:eq(1)").html(data.b);
//ajax.php function that returns block of html checkboxes
<?php
function AddFilterItem($item)
{
echo "<input type='checkbox' class='checkboxes' value=$item checked>$item<br>";
}
function DisplayFilteredItems($order,$sqlSelect) {
$servername = "localhost";
$username="root";
$password="";
$dbname="coasters";
// create connection
$cxn=mysqli_connect($servername,$username,$password,$dbname);
mysqli_query($cxn,"SET NAMES 'utf8'");
//check connection
if(!$cxn)
{
die("Connection failed");
}
$sqlStart="SELECT DISTINCT";
$sqlquery="
FROM brewary
JOIN city ON brewary.Mesto_ID=city.Mesto_ID
JOIN collection ON brewary.Znacka_ID=collection.Znacka_ID
JOIN country ON country.Zeme_ID=city.Zeme_ID
JOIN type ON brewary.Druh_ID=type.Druh_ID
";
if (isset($_GET['ZemeArray'])===true && empty($_GET['ZemeArray'])===false) {
$ZemeArrayChecked=' WHERE country.Zeme IN '.$_GET['ZemeArray'];
}
else {
$ZemeArrayChecked='';
}
if (isset($_GET['MestoArray'])===true && empty($_GET['MestoArray'])===false) {
$MestoArrayChecked=' WHERE city.Mesto IN '.$_GET['MestoArray'];
}
else {
$MestoArrayChecked='';
}
$sqlWhere=
$ZemeArrayChecked.
$MestoArrayChecked
;
$preorder="ORDER BY ";
$order1=$preorder.$order;
$sql=$sqlStart.' '.$sqlSelect.' '.$sqlquery.' '.$sqlWhere.' '.$order1;
$result = mysqli_query($cxn,$sql);
echo"<input type='checkbox' class='VSE' checked>VYBER VŠE<br>";
//$row=mysqli_fetch_array($result, MYSQLI_ASSOC);
while($row = $result->fetch_array())
{
$row=$row;
$rows[] = $row;
}
foreach($rows as $row)
{
AddFilterItem($row["$sqlSelect"]);
}
$result->free();
mysqli_close($cxn);
$pokus=DisplayFilteredItems("Zeme","Zeme");
}
$pokus2=DisplayFilteredItems("Mesto","Mesto");
echo json_encode('a'=>$pokus, 'b'=>$pokus2);
?>