如何捕获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.

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