I have created an 'add to favourites' button successfully and am now having difficulty with 'remove from favourites' part.
I'm not sure of the correct way to handle multiple ajax actions in the PHP file. My code so far looks like this:
HTML
<div class="favourite-links">
<a href="inc/functions?action=remove&..." id="remove">
Remove From Favourites
</a>
<a href="inc/functions.php?action=add&..." id="add">
Add to Favourites
</a>
</div>
JQUERY
$(document).ready(function() {
$('#add').click(function(event) {
event.preventDefault();
var courseData = {...}
$.ajax({
url: "inc/functions.php",
type: "POST",
data: courseData,
success: function() {
$('#add').css('display', 'none');
$('#remove').css('display', 'inline');
alert('successful');
}
});
return false;
});
});
PHP
<?php require("../../Common2.php");
$sessionID = $_COOKIE['PHPSESSID'];
$student = $_POST['userid'];
$courseID = $_POST['courseID'];
$courseName = $_POST['courseName'];
$query = "INSERT IGNORE INTO course_tag_fetch
(sessionID, courseID, courseName, userid)
VALUES
('$sessionID', '$courseID', '$courseName', '$student')";
$query_run = mysql_query($query);
if ($query_run) {
echo 'Success';
} else {
echo 'Failed';
}
?>
I want the remove action to DELETE from the database. Is the best way to put the actions in if statements? E.g.
if (action == "add") {
--- INSERT query --- }
if (action == "remove) {
--- DELETE query ---}
Or is there a more appropriate way to do this? Any help much appreciated.