I have a favourites button calling an ajax request to update a MySQL database.
I would like to have a alert if there are duplicate additions or too many additions.
Can anybody see a way that I could show an alert if there is a duplicate addition? My code is below:
AJAX REQUEST
$.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;}
Just return the text to alert to your 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 request:
$.ajax({
type: 'post',
url: 'favaddDB.php',
data: $('#addfaveform').serialize(),
success: function (message) {
alert(message);
}
});
Additionally, you should consider using JSON, to pass back entire objects to your javascript, and parse it there:
$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
]);
}
And in your ajax, make sure you set dataType: 'json'
, which will automatically parse it into an object:
$.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);
}
}
});
Simple is better, in most cases. By returning messages
, you can do whatever you want on the frontend side, depending on the message.
PHP:
<?php
$db = new PDO("mysql:host=localhost;dbname=favourites", 'root', '');
$query1 = "SELECT * FROM " . $email;
$stat1 = $db->prepare($query1);
$stat1->execute();
$count = $stat1->rowCount();
$fave = $count;
if ($fave > 9) {
echo "tooMany"; exit();
} else {
echo "addedFav"; $fave++;
}
JS:
jQuery.post({
url: 'favaddDB.php',
data: jQuery('#addfaveform').serialize()
}).then(function (code) {
switch (code) {
case "addedFav":
alert('Added To Favourites');
break;
case "tooMany":
alert('Too many favourites');
break;
}
}).catch(function (error) {
console.log(error);
});