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

How to catch array sent by PHP script as Ajax response and use further?

本文关键字:响应 Ajax 进一步 数组 PHP 何捕获 脚本      更新时间:2023-09-26

我正在做一个电子商务项目练习,现在我正在构建产品过滤器。我有三个文件

  1. catalogue.php

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

    product在左边过滤,在右边显示产品。

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

因此product_filter.php返回从数据库检索到的product_id数组,并将其返回给AJAX。现在的问题是,我从AJAX调用中得到的产品id数组,如何在catalog。php中使用?

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

我如何将这个数组传递给我的catalog . PHP页面,以便它可以使用这个数组并在其上调用进一步的PHP函数?

如果问题不清楚,请说出来,我会尽我所能解释清楚。

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

你至少有2个选项


选项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);
             }
         });
     }
 });