[英]JQuery AJAX on Error alert
我有一個CS:GO賭博網站,用戶必須在其中保存他的Steam交易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帖子上顯示此內容? 並將其保存到數據庫。
您可以在其中回顯一些值:
// 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({
...
});
}
嗨,您不會在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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.