Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回多个单词的jquery ajax调用_Php_Jquery_Ajax_Post - Fatal编程技术网

Php 返回多个单词的jquery ajax调用

Php 返回多个单词的jquery ajax调用,php,jquery,ajax,post,Php,Jquery,Ajax,Post,我使用以下代码来使用php返回 if (isset($_POST['P_Id'])) { if (!isset($_SESSION['carted_products'])) { $_SESSION['carted_products'] = array(); }//end if not set products array in session if (!in_array($_POST['P_Id'], $_SESSION['carted_products

我使用以下代码来使用php返回

if (isset($_POST['P_Id'])) {
    if (!isset($_SESSION['carted_products'])) {
        $_SESSION['carted_products'] = array();
    }//end if not set products array in session

    if (!in_array($_POST['P_Id'], $_SESSION['carted_products'])) {
        array_push($_SESSION['carted_products'], $_POST['P_Id']);
        echo "success";
    }//end if not in array
    else {
        echo "already_added";
    }
}//end if not set post value
else {
    echo "fail";
}//end else
现在进入调用页面的jquery部分。我用

$.post(
    "add_to_cart.php",
    {P_Id:"<?php echo $row['P_Id'] ?>"},
    function(responseText){
        //if(responseText === "success"){
        //things to do on success
    }
$.post(
“将_添加到_cart.php”,
{P_Id:”“},
函数(responseText){
//如果(responseText==“成功”){
//成功之道
}
但是if不会返回true

现在,当我在responseText中检查“ready_added”的索引时,它返回6


为什么会发生这种情况。

您的回复中可能有空白,请尝试删减它

if($.trim(responseText) === "success"){

在php中,尝试echo“0”等。 在JS AJAX的成功案例中,请执行以下操作:


您可能在php端回显了一些其他文本(在
之前的空格)。好吧,只需快速浏览一下,我发现您没有关闭jquery帖子,回显后没有分号

$.post("add_to_cart.php",{P_Id:"<?php echo $row['P_Id']; ?>"},function(responseText){
    if(responseText === "success"){
        //things to do on success
    }
});
$.post(“add_to_cart.php”,{P_Id:”},函数(responseText){
如果(responseText==“成功”){
//成功之道
}
});

就我个人而言,我强烈建议以JOSN格式传输数据。我认为这样会更安全且无错误。

您能尝试将
退出();
返回;
放在您的
echo
语句之后吗?
$.post("add_to_cart.php",{P_Id:"<?php echo $row['P_Id']; ?>"},function(responseText){
    if(responseText === "success"){
        //things to do on success
    }
});