weixin_33674437 2017-01-09 10:24 采纳率: 0%
浏览 23

jQuery AJAX错误警告

I have a CS:GO gamble site where user have to save his Steam Trade URL, i have made an JQuery AJAX post to save the data on database:

$(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
                }
            });
        }
    });
});

If the data was submitted correctly it gives that alert, but my tradeURL.php contains this code:

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 ?

    }
}

So how i should make an alert to that part where it says // HOW TO OUTPUT THIS ON THE AJAX POST ? and it would save it to database.

  • 写回答

2条回答 默认 最新

  • ??yy 2017-01-09 10:35
    关注

    You can echo some value there:

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

    Now in the success callback use this:

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

报告相同问题?