javascript - How to catch array sent by PHP script as Ajax response and use further? -
i working on e-commerce project practice , right building product filters. have 3 files
catalogue.php
- it shows products.
product filters on left , displays products on right. when user checks box ajax call made.
productsfilter.js
it contains javascript , 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); } });
product_filter.php
it php script called ajax call.
$tcheck = $objform->getpost('tcheck'); if(!empty($tcheck)) { if(strstr($tcheck,',')) { $data1 = explode(',',$tcheck); $tarray = array(); foreach($data1 $t) { $tarray[] = "adv.attribute_deterministic_id = $t"; } $where[] = '('.implode(' or ',$tarray).')'; } else { $where[] = '(adv.attribute_deterministic_id = '.$tcheck.')'; } } $w = implode(' , ',$where); if(!empty($w)) { $w = 'where '.$w; } $results = $objcatalogue->getresults($w); echo json_encode($results);
so product_filter.php returns array of product_id
s retrieved database , gives ajax. problem is: array of product ids got ajax call, how use in catalogue.php?
as got {["product_id" : "1"]}
product_filter.php, want use id in catalogue.php , find related attributes , display product details.
how can pass array catalogue.php page can use array , call further php functions on it?
if question unclear kindly so, , try explain can. appreciated.
it seems want data 1 php , send different php page have ajax callback process results second page.
you have @ least 2 options
option 1 (the way it)
in product_filter.php
, near top,
include('catalogue.php');
still in product_filter.php
somewhere have function
function getfilterstuff($somedatafromajax){ // ... stuff here filter or whatever $productdata = getcataloguestuff($results); echo json_encode($productdata); exit; }
in catalogue.php
somewhere have function
function getcataloguestuff($resultsfromfilter){ // ... product data here return $productdata; }
then in ajax this:
$.ajax({ type: "post", datatype: "json", // add url: "mod/filter_products.php", data: main_string, cache: false, success: function (response) { replyval = response; alert(replyval); } });
option 2
nested ajax calls this:
$.ajax({ type: "post", datatype: "json", // add url: "mod/filter_products.php", data: main_string, cache: false, success: function (filterresponse) { $.ajax({ type: "post", datatype: "json", // add url: "catalogue.php", data: filterresponse, cache: false, success: function (catalogueresponse) { alert(catalogueresponse); } }); } });
Comments
Post a Comment