Reputation: 137
On my website I want to allow users to favorite posts. A logged in user is directed to a page which shows all the posts and under each one I placed a hyperlink to favorite. I want the text to change from favorite to favorited and the other way around. How do I do that?
HTML and PHP
<?php
session_start();
require_once('connection.php');
mysql_select_db($database_connection, $connection);
$query_favorite = "SELECT username, post_id FROM favorite";
$favorite = mysql_query($query_favorite, $connection) or die(mysql_error());
$row_favorite = mysql_fetch_assoc($favorite);
$totalRows_favorite = mysql_num_rows($favorite);
?>
<a href="#" class="favourite">Favourite</a>
Tables in my datatbase
CREATE TABLE `user` (
`username` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`profilepic` varchar(50) DEFAULT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `post` (
`post_id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`dato` date NOT NULL,
`category` varchar(100) NOT NULL,
`description` varchar(500) NOT NULL,
`text` longtext NOT NULL,
PRIMARY KEY (`post_id`),
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
CREATE TABLE `favorite` (
`username` varchar(45) NOT NULL,
`post_id` int(11) NOT NULL,
PRIMARY KEY (`username`,`post_id`),
KEY `fk_favorite_post1_idx` (`post_id`),
CONSTRAINT `fk_favorite_user` FOREIGN KEY (`username`) REFERENCES `user` (`username`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_favorite_post1` FOREIGN KEY (`post_id`) REFERENCES `post` (`post_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Upvotes: 1
Views: 1234
Reputation: 880
PHP
<?php
session_start();
require_once('connection.php');
mysql_select_db($database_connection, $connection);
$query_favorite = "SELECT username, post_id FROM favorite";
$favorite = mysql_query($query_favorite, $connection) or die(mysql_error());
$row_favorite = mysql_fetch_assoc($favorite);
$totalRows_favorite = mysql_num_rows($favorite);
if(in_array($_POST['id'], $row_favorite))
{
//is already favourited, run a query to remove that row from the db, so it won't be favorited anymore
}
else
{
//post is not favourited already, run a query to add a new favourite to the db.
}
?>
HTML
<a href="#" class="favourite" data-id="<?php echo $post_id; ?>">Favourite</a>
jQuery
$(document).ready(function() {
$('.favourite').on('click', null, function() {
var _this = $(this);
var post_id = _this.data('id');
$.ajax({
type : 'POST',
url : '/file.php',
dataType : 'json',
data : 'id='+ post_id,
complete : function(data) {
if(_this.siblings('.typcn-star-outline'))
{
_this.html('<span class="typcn typcn-star-full-outline"></span>Favourite');
}
else
{
_this.html('<span class="typcn typcn-star-outline"></span>Favourited');
}
}
});
});
});
Also, please note that in PHP mysql_* functions have been deprecated and are unsafe to use (they allow for SQL Injection attacks). Learn more about PDO here: http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059
Upvotes: 2