weixin_33671935 2015-09-25 09:05 采纳率: 0%
浏览 14

将数据从ajax传递到php [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call" dir="ltr">How do I return the response from an asynchronous call?</a>
                            <span class="question-originals-answer-count">
                                (38 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2015-09-28 05:40:24Z" class="relativetime">4 years ago</span>.</div>
        </div>
    </aside>

Ajax:

function check_user_country_prod(userId , countryCode  , testType )
{ //using alert to check that all data are correct
    $.ajax({
    type: "POST",

    url: "http://localhost/test/testForm.php",

    data: { userId: userId , 
            countryCode : countryCode  ,
             productCode:  testType
        },
    success:function(res) {
        if(res == "OK")
            return true;    
        else
            return false;
    }
});
}

PHP:

<?php
    require_once("Connections/cid.php");

    $userId= $_POST['userId'];
    $productCode= $_POST['productCode'];
    $countryCode= $_POST['countryCode'];

    $sql_check = "SELECT * FROM utc WHERE userId = '$userId' AND productCode = '$productCode' AND countryCode = '$countryCode'";

    $list = mysqli_query( $conn, $sql_check);
    $num  = mysqli_fetch_assoc($list);

    if($num >0)
        echo "OK";

    else
        echo "NOK";

?>

I am very sure that the data i had pass in to the php file are correct. However i cant seem to get my data to the php file and it keep return false value back to me. Anything i can do to make it works?

**Note: Somehow even if i change both result to return true in ajax, it will still return false. and i tried to change the link to another file with only echo "OK"; but it also doesn't work. So i think it is not the file problem. It just never run the ajax no matter what. Do i need to do any link for ajax to run? **

</div>
  • 写回答

4条回答 默认 最新

  • weixin_33713503 2015-09-25 09:13
    关注
    function check_user_country_prod(userId , countryCode  , testType )
    { //using alert to check that all data are correct
        $.ajax({
            url: "test/testForm.php"  
            type: "POST",
    
            url: "http://localhost/test/testForm.php", // This needs to be "test/testForm.php"
    
            data: { userId: userId , 
                countryCode : countryCode  ,
                 productCode:  testType
            },
            success:function(res) {
            if(res == "OK")
                return true;    
            else
                return false;
            }
        });
    }
    
    评论

报告相同问题?