如何捕获PHP脚本发送的数组作为Ajax响应并进一步使用?

时间:2015-08-30 17:33:21

标签: javascript php jquery ajax

我正在开展一个电子商务项目,现在我正在构建产品过滤器。所以我有三个文件

  1. catalogue.php

    • 它基本上显示了所有产品。

    左侧的产品过滤器,右侧显示产品。当用户选中一个方框时,就会调用AJAX。

  2. productsfilter.js

    • 它包含Javascript和AJAX调用。

      var themearray = new Array();       
      $('input[name="tcheck"]:checked').each(function(){          
      themearray.push($(this).val());     
      });
      if(themearray=='') $('.spanbrandcls').css('visibility','hidden');
      var theme_checklist = "&tcheck="+themearray;
      
      var main_string = theme_checklist;
      main_string = main_string.substring(1, main_string.length)
      $.ajax({
      type: "POST",
      url: "mod/product_filter.php",
      data: main_string, 
      cache: false,
      success: function(html){
          replyVal = JSON.parse(myAjax.responseText);     
          alert(replyVal);                
      }
      });
      
  3. product_filter.php

    • 这是由AJAX调用调用的PHP脚本。

        $tcheck = $objForm->getPost('tcheck');
        if(!empty($tcheck)) {
          if(strstr($tcheck,',')) {
            $data1 = explode(',',$tcheck);
            $tarray = array();
            foreach($data1 as $t) {
            $tarray[] = "adv.attribute_deterministic_id = $t";
        }
        $WHERE[] = '('.implode(' OR ',$tarray).')';
        } else {
           $WHERE[] = '(adv.attribute_deterministic_id = '.$tcheck.')';
        }
       }
       $w = implode(' AND ',$WHERE);
       if(!empty($w)) 
       {
          $w = 'WHERE '.$w;
       }
       $results = $objCatalogue->getResults($w);
       echo json_encode($results);
      
  4. 因此,product_filter.php返回从数据库中检索到的product_id数组,并将其返回给AJAX。现在的问题是:我从AJAX调用得到的那个产品ID数组,如何在catalogue.php中使用它?

    当我从product_filter.php获得{["product_id" : "1"]}时,我想在catalogue.php中使用此ID并查找相关属性并显示产品详细信息。

    如何将此数组传递给我的catalogue.php页面,以便它可以使用此数组并在其上调用更多PHP函数?

    如果问题不明确,请善意地说出来,我会尽可能清楚地解释它。非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

似乎你想从一个php获取数据并将其发送到另一个php页面,然后让ajax回调处理第二页的结果。

您至少有两个选项

选项1(我会采用的方式)

product_filter.php附近,靠近顶部,执行此操作

include('catalogue.php');

仍在product_filter.php某个地方有你的功能

function getFilterStuff($someDataFromAjax){
    // ... do some stuff here to filter or whatever
    $productData = getCatalogueStuff($results);
    echo json_encode($productData);
    exit;
}

catalogue.php某个地方有这个功能

function getCatalogueStuff($resultsFromFilter){
    // ... get product data here
    return $productData;
}

然后在你的Ajax中执行此操作:

$.ajax({
    type: "POST",
    dataType: "json", // add this
    url: "mod/filter_products.php",
    data: main_string,

    cache: false,
    success: function (response) {
        replyVal = response;
        alert(replyVal);
    }
});

选项2

嵌套的ajax调用如下:

  $.ajax({
     type: "POST",
     dataType: "json", // add this
     url: "mod/filter_products.php",
     data: main_string,
     cache: false,
     success: function (filterResponse) {
         $.ajax({
             type: "POST",
             dataType: "json", // add this
             url: "catalogue.php",
             data: filterResponse,
             cache: false,
             success: function (catalogueResponse) {
                 alert(catalogueResponse);
             }
         });
     }
 });