[英]JQuery AJAX on Error alert
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: 我有一个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
}
});
}
});
});
If the data was submitted correctly it gives that alert, but my tradeURL.php contains this code: 如果数据提交正确,它将发出警报,但我的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 ?
}
}
So how i should make an alert to that part where it says // HOW TO OUTPUT THIS ON THE AJAX POST ? 那么,我应该如何提醒该部分显示//如何在AJAX帖子上显示此内容? and it would save it to database.
并将其保存到数据库。
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({
...
});
}
Hi you dont output alerts for JS within PHP. 嗨,您不会在PHP中为JS输出警报。 Your php should return something like:
您的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.
And then within your javascript you interpret your responce: 然后在您的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.