dongleiwei2182 2015-08-30 17:33
浏览 48

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

I am working on an e-commerce project for practice and right now I am building product filters. So I have three files

  1. catalogue.php

    • It basically shows all the products.

    product filters on left and displays products on right. When user checks a box then AJAX call is made.

  2. productsfilter.js

    • It contains Javascript and AJAX calls.

      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

    • It is the PHP script called by the AJAX call.

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

So product_filter.php returns an array of product_ids retrieved from the database and gives it back to AJAX. Now the problem is: that array of product ids I got from AJAX call, how do I use it in catalogue.php?

As I got {["product_id" : "1"]} from product_filter.php, I want to use this id in catalogue.php and find the related attributes and display the product details.

How can I pass this array to my catalogue.php page so that it can use this array and call further PHP functions on it?

If the question is unclear then kindly say so, and I will try to explain it as clearly as I can. Help would be much appreciated.

  • 写回答

1条回答 默认 最新

  • doujiu5464 2015-08-30 17:42
    关注

    It seems you want to get data from one php and send it to a different php page then have the ajax callback process the results from that second page.

    You have at least 2 options


    Option 1 (the way I would do it)

    In product_filter.php, near the top, do this

    include('catalogue.php');

    still in product_filter.php somewhere have your function

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

    In catalogue.php somewhere have that function

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

    Then in your Ajax do this:

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

    Option 2

    Nested ajax calls like this:

      $.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);
                 }
             });
         }
     });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?