gpt4 book ai didi

php - 在 AJAX 调用中显示来自 PHP 的消息

转载 作者:行者123 更新时间:2023-11-29 17:16:55 25 4
gpt4 key购买 nike

我有一个收藏夹按钮,调用 ajax 请求来更新 MySQL 数据库。

如果有重复添加或添加过多,我希望收到警报。

任何人都可以找到一种方法,如果有重复添加,我可以显示警报吗?我的代码如下:

AJAX 请求

$.ajax({
type: 'post',
url: 'favaddDB.php',
data: $('#addfaveform').serialize(),
success: function () {
alert('Added To Favourites');
}
});

PHP

$db = new PDO("mysql:host=localhost;dbname=favourites", 'root', '');                                                                  
$query1="SELECT * FROM `$email` ";
$stat1=$db->prepare($query1);
$stat1->execute();// IMPORTANT add PDO variables here to make safe

//Check if fave adds >9
$count = $stat1->rowCount();
$fave=$count;
if ($fave>9) {die(); exit();} // HERE I WISH TO RUN AN ALERT OR SEND BACK A MESSAGE TO DISPLAY
else {$fave=$fave+1;}

最佳答案

只需返回文本即可提醒您的 JavaScript:

$db = new PDO("mysql:host=localhost;dbname=favourites", 'root', '');                                                                  
$query1="Query here ($email/similar should NOT BE HERE! Add them via execute/prepare.";
$stat1=$db->prepare($query1);
$stat1->execute();// IMPORTANT add PDO variables here to make safe

//Check if fave adds >9
$count = $stat1->rowCount();
$fave=$count;
if ($fave>9) {die("Here is a message");} // HERE I WISH TO RUN AN ALERT OR SEND BACK A MESSAGE TO DISPLAY
else {$fave=$fave+1; die("Here is another message"); }

Ajax 请求:

$.ajax({
type: 'post',
url: 'favaddDB.php',
data: $('#addfaveform').serialize(),
success: function (message) {
alert(message);
}
});

此外,您应该考虑使用 JSON,将整个对象传递回您的 JavaScript,并在那里解析它:

$db = new PDO("mysql:host=localhost;dbname=favourites", 'root', '');                                                                  
$query1 = "Query here ($email/similar should NOT BE HERE! Add them via execute/prepare.";
$stat1 = $db->prepare($query1);
$result = $stat1->execute();// IMPORTANT add PDO variables here to make safe

// Tell javascript we're giving json.
header('Content-Type: application/json');

if (!$result) {
echo json_encode(['error' => true, 'message' => 'A database error has occurred. Please try again later']);
exit;
}

//Check if fave adds >9
$count = $stat1->rowCount();
$fave = $count;
if ($fave > 9) {
echo json_encode(['error' => false, 'fave' => $fave, 'message' => 'Fave > 9!']);
} // HERE I WISH TO RUN AN ALERT OR SEND BACK A MESSAGE TO DISPLAY
else {
$fave = $fave+1;
echo json_encode([
'error' => false,
'fave' => $fave,
'message' => 'Current fave count: ' . $fave
]);
}

在你的ajax中,确保设置dataType: 'json',它会自动将其解析为一个对象:

$.ajax({
type: 'post',
url: 'favaddDB.php',
data: $('#addfaveform').serialize(),
dataType: 'JSON',
success: function (res) {
if (res.error) {
//Display an alert or edit a div with an error message
alert(res.message);
} else {
//Maybe update a div with the fave count
document.getElementById('#favcount').value = res.fave;
alert(res.message);
}
}
});

关于php - 在 AJAX 调用中显示来自 PHP 的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51523737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com