错误警报上的JQuery AJAX

时间:2017-01-09 10:24:38

标签: javascript php jquery ajax

我有一个CS:GO赌博网站,用户必须保存他的Steam Trade URL,我已经发了一个JQuery AJAX帖子来保存数据库中的数据:

$(document).on('click', '#saveURL', function(e) {
    var data = $("#tradeURL").serialize();
    $.ajax({
        data: data,
        type: "post",
        url: "functions/tradeURL.php",
        success: function(data) {
            noty({
                text: '<b>Success!</b> <br>Your Trade URL is now saved.',
                layout: 'topRight',
                theme: 'metroui',
                type: 'success',
                maxVisible: 10,
                timeout: 5000,
                closeWith: ['click', 'timeout'],
                animation: {
                    open: {
                        height: 'toggle'
                    },
                    close: {
                        height: 'toggle'
                    },
                    easing: 'swing',
                    speed: 500 // opening & closing animation speed
                }
            });
        }
    });
});

如果数据已正确提交,则会提供该提醒,但我的 tradeURL.php 包含此代码:

if (isset($_REQUEST)) {
    $tradeURL = $_POST["tradeURL"];
    $tradeURL = htmlspecialchars(str_replace('', '', $tradeURL));
    $turl = explode("=", explode("&", "'.$tradeURL.'") [0]); // user trade url
    if ($turl[1] == $steamid - intval(0x0110000100000000)) {
        $update = "UPDATE users SET tlink='$tradeURL' WHERE steamid=$steamid";
        if ($db->query($update) === TRUE) {

            // THIS GIVES THE ALERT ON THE JQUERY SCRIPT

        }
        else {
            echo "Error updating record: " . $db->error;
        }
    }
    else {

        // HOW TO OUTPUT THIS ON THE AJAX POST ?

    }
}

那么我应该如何向那个部分发出警告 //如何在AJAX POST上输出?并将它保存到数据库中。

2 个答案:

答案 0 :(得分:0)

你可以在那里回应一些价值:

// HOW TO OUTPUT THIS ON THE AJAX POST ?
echo "Invalid value."; // or your choice of text...

现在在成功回调中使用此:

success: function(data) {
    if(data === 'Invalid value.'){ // <--add this one
       alert(data);
       return;
    }
    noty({
        ...
    });
}

答案 1 :(得分:0)

嗨,你不要在PHP中输出JS的警报。你的php应该返回类似的东西:

header('Content-Type: application/json');
if($correct){
    echo json_encode(array("status" => true));
}else{
    echo json_encode(array("status" => false));
}
die; // note: this should not be used, instead you shoud make sure there is no other output on page.

然后在您的javascript中解释您的回复:

if(data.status == true){
    //display message that input accepted
} else {
    //display message that input is not valid
}