ajax无法通过PHP检索已分析的数组 [英] ajax is not able to retrieve the parsed array by the PHP
问题描述
我在一个弹出窗口中设置了一个表格,用于显示由特定用户ID发出的订单。
< div id = shpcart>
< div id =shpop>
< table>
< thead>
< th>第th>< th>项目名称< th colspan =" 2>> Price< th>< th> shipping< / th> ;< / TR><的第i;数量< /第>
< / thead>
< tbody id =cartbody>
< / tbody>
< / table>
< / div>
< / div>
以下是将用户ID发送到服务器的ajax
$(function(){
$(。prod_buy)。click(function(){
var htmlId = $(this).attr('id');
var idarray = htmlId.split(' - ');
var itemId = idarray [1];
$ .ajax ({
type:POST,
url:tempselector.php,
data:{'proId':itemId}
})。done(function(msg) {
jQuery.parseJSON(msg);
var output = jQuery.parseJSON(msg);
var htmlstring;
alert(im running);
for (var index = 0; index< output.length; index ++){
var itmnam = output [index] [1];
var itmpic = output [index] [2];
var itmpr = output [index] [3];
var itmdisc = output [index] [4];
var itmdesc = output [index] [5];
var itmshp = output [index] ] [6];
var itmav = output [index] [7];
htmlstring + =< tr>< th>< img src ='../cartimg /' + itmpic + >< /第> < TD> 中+ itmnam + < / TD>< TD> 中+ itmdisc + < / TD>< TD> 中+ itmshp + < / TD>< TD> QTY< / TD>< / TR>中;
}
$('#cartbody')。html(htmlstring);
$(#shpcart)。fadeIn();
});
});
这里是PHP获取传递的用户id的顺序
<?php
session_start();
include('includes / config.php');
$ uId = $ _SESSION [uId];
$ prID = mysqli_real_escape_string($ link,$ _ POST [proId]);
// $ pQty = mysqli_real_escape_string($ link,$ _ POST [prQTY]);
$ pQty = 2;
// $ prID = 4;
$ sqlget =SELECT * FROM vasplus_programming_blog_pagination WHERE id ='。$ prID。'; //找到所选项目
$ resultget = mysqli_query($ link,$ sqlget);
$ itemget = mysqli_fetch_array($ resultget);
$ itemId = $ itemget [0]; //将选定的ID存储在var
$ itemNm = $ itemget [1]中;
$ ITimage = $ itemget [2];
$ ITprice = $ itemget [3];
// $ ITdiscount = $ itemget [4];
$ ITdescription = $ itemget [5];
$ ITshipping = $ itemget [6];
// $ ITavailable = $ itemget [7];
$ ITcontrycod = $ itemget [8];
$ itemCol = $ itemget [9]; //将选定的颜色存储在var
$ itemSiz = $ itemget [10]; //将选定的大小存储在var
$ ITqty = $ itemget [11];
//我们需要搜索临时表以查看所选项是否存在
$ sqlsrch =SELECT * FROM XXXXX WHERE product_id ='。$ prID。'AND size ='。 $ itemSiz。'AND color ='。$ itemCol。'AND user_id ='。$ uId。'; //如果该项目在临时表中或
$ resultsrch = mysqli_query($ link,$ sqlsrch);
$ itemsrch = mysqli_fetch_array($ resultsrch);
echo $ itemsrch;
if(isset($ itemsrch)){
$ adqty = $ itemsrch [8];
$ adqty ++;
$ sqlupdate =UPDATE XXXXXX SET qty ='。$ adqty。'WHERE product_id ='。$ prID。'AND size ='。$ itemSiz。'AND color ='。$ itemCol。'和user_id ='。$ uId。'; //更新临时表中现有项目的数量
$ resultupdate = mysqli_query($ link,$ sqlupdate);
} else {
echouser id searching;
$ sqlisUsr =选择*从XXXXXX WHERE user_id ='。$ uId。'; //检查用户是否有临时表中的任何项目
$ resultisUsr = mysqli_query($ link,$ sqlisUsr);
$ isUsr = mysqli_fetch_array($ resultisUsr);
if(isset($ isUsr)){//如果用户在购物车中有物品
$ getOrdId = $ isUsr [2]; //获取订单ID
$ sqladdN =INSERT INTO XXXXXXx(order_id,user_id,photo,express,qty,unit_price,country,color,size,product_id)VALUES('$ getOrdId',' $ uId','$ ITimage','$ ITshipping','$ pQty','$ ITprice','$ ITcontrycod','$ itemCol','$ itemSiz','$ prID'); //插入现有订单ID
$ resultaddN = mysqli_query($ link,$ sqladdN); } else {//用户没有记录temp
echoelse is running;
$ ReNth = 0;
$ oId = 1;
while($ ReNth!= 1){
$ sqlNewOiD =SELECT * FROM XXXXXX WHERE order_id ='。$ oId。'; //产生一个新的订单ID
$ resultOsrch = mysqli_query($ link,$ sqlNewOiD);
$ oIdsrch = mysqli_fetch_array($ resultOsrch);
if(isset($ oIdsrch)){
echo $ oId ++;
echoorder Id generated。$ oId;
} else {//在临时表中添加新订单ID的新项目
echo $ oId。oId< br /> ;
echo $ uId。uId< br /> ;
echo $ ITimage。< br /> ;
echo $ ITshipping。< br /> ;
echo $ pQty。< br /> ;
echo $ ITprice。< br /> ;
echo $ ITcontrycod。< br /> ;
echo $ itemCol。< br /> ;
echo $ itemSiz。< br /> ;
echo $ prID。< br /> ;
$ b $ sqladdNOID =INSERT INTO XXXXXx(order_id,user_id,photo,express,qty,unit_price,country,color,size,product_id)VALUES('$ oId','$ uId','$ ITimage','$ ITshipping','$ pQty','$ ITprice','$ ITcontrycod','$ itemCol','$ itemSiz','$ prID');
$ resultaddNOID = mysqli_query($ link,$ sqladdNOID);
$ ReNth = 1; //退出搜索唯一的订单ID循环
} //结束如果
} //结束而
} //结束if
} // end if
//购物车的json代码
$ sql =SELECT * FROM XXXXX WHERE user_id ='。$ uId。';
$ result = mysqli_query($ link,$ sql);
while($ item = mysqli_fetch_array($ result)){
$ array [] = $ item;
}
echo json_encode($ array);
?>
问题是ajax无法通过PHP检索已分析的数组。我发现$ uId被传递给PHP和PHP代码工作正常,$ array已被提取,但作为回报,ajax无法读取$数组。
$ b $如果不必要的输出存在,那么json不能被解析,这就是问题所在。
I have a table sets in a popup window to show orders placed by a specific userID
<div id="shpcart">
<div id="shpop">
<table>
<thead>
<tr><th></th><th>Item name</th><th colspan="2">Price</th><th>shipping</th></tr><th>Quantity</th>
</thead>
<tbody id= "cartbody">
</tbody>
</table>
</div>
</div>
Here is the ajax to send the userID to the server
$(function(){
$(".prod_buy").click(function(){
var htmlId = $(this).attr('id');
var idarray = htmlId.split('-');
var itemId = idarray[1];
$.ajax({
type: "POST",
url: "tempselector.php",
data: {'proId': itemId }
}).done(function( msg ) {
jQuery.parseJSON(msg);
var output = jQuery.parseJSON(msg);
var htmlstring;
alert ("im running");
for(var index=0;index<output.length; index++){
var itmnam = output[index][1];
var itmpic = output[index][2];
var itmpr = output[index][3];
var itmdisc = output[index][4];
var itmdesc = output[index][5];
var itmshp = output[index][6];
var itmav = output[index][7];
htmlstring +="<tr><th><img src = '../cartimg/'"+itmpic+"></th><td>"+itmnam+"</td><td>"+itmdisc+"</td><td>"+itmshp+"</td><td>QTY</td></tr>";
}
$('#cartbody').html(htmlstring);
$("#shpcart").fadeIn();
});
});
and here is the PHP to fetch the order of the passed user id
<?php
session_start();
include('includes/config.php');
$uId = $_SESSION["uId"];
$prID = mysqli_real_escape_string($link,$_POST["proId"]);
//$pQty = mysqli_real_escape_string($link,$_POST["prQTY"]);
$pQty = 2;
//$prID = 4;
$sqlget= "SELECT * FROM vasplus_programming_blog_pagination WHERE id='".$prID."'"; // to find the selected item
$resultget = mysqli_query($link, $sqlget);
$itemget = mysqli_fetch_array($resultget);
$itemId = $itemget[0]; // store the selected id in var
$itemNm = $itemget[1];
$ITimage = $itemget[2];
$ITprice = $itemget[3];
//$ITdiscount =$itemget[4];
$ITdescription =$itemget[5];
$ITshipping =$itemget[6];
// $ITavailable = $itemget[7];
$ITcontrycod =$itemget[8];
$itemCol = $itemget[9]; // store the selected color in var
$itemSiz = $itemget[10]; // store the selected size in var
$ITqty = $itemget[11];
// we need to search the temp table to see if the selected item is there
$sqlsrch= "SELECT * FROM XXXXX WHERE product_id ='".$prID."' AND size = '".$itemSiz."' AND color = '".$itemCol."' AND user_id = '".$uId."' "; // if the item is in the temp table or not
$resultsrch = mysqli_query($link, $sqlsrch);
$itemsrch = mysqli_fetch_array($resultsrch);
echo $itemsrch;
if (isset($itemsrch)){
$adqty = $itemsrch[8];
$adqty ++;
$sqlupdate=" UPDATE XXXXXX SET qty='".$adqty."' WHERE product_id ='".$prID."' AND size = '".$itemSiz."' AND color = '".$itemCol."' AND user_id = '".$uId."' "; // update the qty of theexisting items in temp table
$resultupdate = mysqli_query($link, $sqlupdate);
}else {
echo " user id searching ";
$sqlisUsr= "SELECT * FROM XXXXXX WHERE user_id = '".$uId."' "; // check if the user has any item in the temp table
$resultisUsr = mysqli_query($link, $sqlisUsr);
$isUsr = mysqli_fetch_array($resultisUsr);
if (isset($isUsr)){ // if user has items in the cart
$getOrdId = $isUsr[2]; // get the order ID
$sqladdN=" INSERT INTO XXXXXXx (order_id, user_id, photo, express, qty, unit_price, country, color, size, product_id) VALUES ('$getOrdId', '$uId' , '$ITimage' , '$ITshipping' , '$pQty', '$ITprice' , '$ITcontrycod' , '$itemCol' , '$itemSiz' , '$prID' ) "; // insert the item with the existing order ID
$resultaddN = mysqli_query($link, $sqladdN); }else{ // user has no record in temp order
echo " else is running " ;
$ReNth = 0;
$oId = 1;
while ($ReNth != 1){
$sqlNewOiD= "SELECT * FROM XXXXXX WHERE order_id = '".$oId."'"; // generate a new order ID
$resultOsrch = mysqli_query($link, $sqlNewOiD);
$oIdsrch = mysqli_fetch_array($resultOsrch);
if (isset($oIdsrch)){
echo $oId++;
echo " order Id generated " .$oId;
}else{ // insert the new item with the new order id in the temp table
echo $oId."oId<br />" ;
echo $uId."uId<br />" ;
echo $ITimage."<br />" ;
echo $ITshipping."<br />" ;
echo $pQty."<br />" ;
echo $ITprice."<br />" ;
echo $ITcontrycod."<br />" ;
echo $itemCol."<br />" ;
echo $itemSiz."<br />" ;
echo $prID."<br />" ;
$sqladdNOID = " INSERT INTO XXXXXx (order_id, user_id, photo, express, qty, unit_price, country, color, size, product_id) VALUES ('$oId', '$uId' , '$ITimage' , '$ITshipping' , '$pQty', '$ITprice' , '$ITcontrycod' , '$itemCol' , '$itemSiz' , '$prID' ) ";
$resultaddNOID = mysqli_query($link, $sqladdNOID);
$ReNth = 1; // quit the searching for unique order id loop
}//end if
}//end while
}// end if
}// end if
// pars json code for the cart
$sql= "SELECT * FROM XXXXX WHERE user_id = '".$uId."'" ;
$result = mysqli_query($link, $sql);
while($item = mysqli_fetch_array($result)){
$array[] = $item;
}
echo json_encode($array);
?>
The problem is that the ajax is not able to retrieve the parsed array by the PHP. I see that the $uId being passed to the PHP and the PHP code works fine and $array has been fetched, but in return the ajax isn't able to read the $array .
please help me here
if unnecesary output is present then json can not be parsed, that's the issue. problem has been solved
这篇关于ajax无法通过PHP检索已分析的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!