AJAX - 将帖子更改为getJSON

时间:2016-09-18 20:18:35

标签: php jquery html ajax

我有以下jquery使用POST方法并从ajax.php文件返回HTML数据(复选框)并且效果很好。
你能否告诉我如何将方法更改为.getJSON以便我可以从PHP中检索多个数组?我对AJAX主题很陌生并且已经尝试了很多方法,但没有任何方法对我有用。

// jquery

$.post('ajax.php', {ZemeArray,MestoArray},function(data) { 
    $(".CHCB:eq(0)").html(data);

//返回html复选框块的ajax.php函数

DisplayFilteredItems("Zeme","Zeme");

我已经尝试按如下方式更改它,但返回HTML代码的PHP函数是错误的:

// jquery的

$.getJSON('ajax.php', {ZemeArray,MestoArray},function(data) { 
    $(".CHCB:eq(0)").html(data.a);
    $(".CHCB:eq(1)").html(data.b);

//返回html复选框块的ajax.php函数

<?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);
?>

1 个答案:

答案 0 :(得分:0)

DisplayFilteredItemsAddFilterItem需要返回值,而不是回显结果。

<?php

function AddFilterItem($item)
{
    return "<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);


    $output = "<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)
        {
            $output .= AddFilterItem($row["$sqlSelect"]);
        }

    $result->free();                                

    mysqli_close($cxn);

    return $output;

}
$pokus=DisplayFilteredItems("Zeme","Zeme");
$pokus2=DisplayFilteredItems("Mesto","Mesto");

echo json_encode('a'=>$pokus, 'b'=>$pokus2);
?>