2016-09-18 39 views
-2

我有下面的jQuery使用POST方法,並從ajax.php文件返回HTML數據(複選框),效果很好。
您能否請告知我如何將方法更改爲.getJSON,以便從PHP檢索多個數組?我對AJAX主題非常陌生,並且已經嘗試了很多方法,但沒有爲我工作。AJAX - 更改發佈獲取JSON

// jQuery的

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

//ajax.php函數,返回HTML複選框的塊

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

//ajax.php函數,返回HTML複選框

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

您是否改變PHP腳本使用'$ _GET'而不是'$ _POST'來獲取參數? – Barmar

+0

'DisplayFilteredItems()'返回HTML還是回顯它?您需要它來返回HTML,以便將其放入數組中。 – Barmar

+1

您需要發佈代碼的所有相關部分。 – Barmar

回答

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

謝謝Barmar,這已經解決了! – Jar