如何捕获PHP脚本发送的数组作为Ajax响应并进一步使用
How to catch array sent by PHP script as Ajax response and use further?
我正在做一个电子商务项目练习,现在我正在构建产品过滤器。我有三个文件
-
catalogue.php
- 它基本上显示所有的产品。
product在左边过滤,在右边显示产品。
-
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); } });
-
-
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);
}
});
}
});
相关文章:
- 如何从php返回php变量值以及html响应ajax
- PHP:响应 AJAX 发布请求
- 从 php 响应 AJAX 的更好方法
- 如何将响应 ajax 发送到 html
- 处理服务器响应AJAX
- Twitter Bootstrap Rails按钮下拉列表没有响应AJAX
- 状态200 OK,相同的域,有效的JSON数据,没有响应(Ajax)
- 多个JSON数组作为响应-AJAX&PHP
- 稍后再响应AJAX请求
- Jquery的JSON响应.ajax四舍五入较大的数字,所以我不能得到正确的值
- 如何响应AJAX调用没有erb.js /为什么使用erb.js
- 是否有可能得到php echo/响应Ajax而不通过Ajax张贴
- 如何发送响应ajax到变量javascript
- Servlet不响应ajax请求
- 为什么嵌入的JavaScript在响应ajax调用时不执行?
- 多响应AJAX请求
- JAVASCRIPT:在本地主机上成功响应Ajax后重定向到主页
- Spring响应ajax: 403错误
- JSON响应AJAX格式错误
- 我如何发送压缩(gzip) JSON作为响应Ajax请求,从Java