weixin_33734785 2016-09-18 20:18 采纳率: 0%
浏览 21

AJAX-将Post更改为getJSON

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);
?>
  • 写回答

1条回答 默认 最新

  • weixin_33712987 2016-09-21 18:21
    关注

    DisplayFilteredItems and AddFilterItem need to return values, not echo their results.

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

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计