Example of using php combined with ajax to realize like, like and dislike functions_PHP tutorial

WBOY
Release: 2016-07-13 10:29:47
Original
827 people have browsed it

php is used more and more widely, and in order to increase the richness of the website, many new technologies have emerged. Ajax is an indispensable technology in modern websites. It can refresh data asynchronously and achieve many effects, such as refreshing verification codes and the like function in Weibo, all using this.

Rendering of this like function:

Example of using php combined with ajax to realize like, like and dislike functions_PHP tutorial

Homepage file (index.php):

Copy code The code is as follows:

< ;script type="text/javascript" src="http://www.jb51.net/js/jquery.min.js">

header("Content-type:text/html;charset=utf-8");
include "finger_ajax. php";

$sql = "select * from finger_ajax";
$res = mysql_query($sql,$link);
while($row = mysql_fetch_array($res)){
echo "< p>".$row['title']." rnLike(".$row['finger'].")

rn";
}
?>

Processing ajax requests and configuration information files (finger_ajax.php):

Copy code The code is as follows:

/**"Like" function responds to ajax request*/
//Configuration
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "dddddd";
$dbName = "test";
$dbCharset = "utf8";

$link = mysql_connect($dbHost,$dbUser,$dbPass) or die(mysql_error());
mysql_query("set names ".$dbCharset);
mysql_select_db($dbName);
// End

//Accept the corresponding id
if(!empty($_POST['id'])){
$id = $_POST['id'];
// "Like" addition 1
$sql = "update finger_ajax set finger=finger+1 where id=$id;";
if(mysql_query($sql,$link)){
echo "ok";
}else{
echo "failed";
}
}
?>


js file (finger_ajax.js):

Copy code The code is as follows:

//Like js
function finger(topic_id){
$.post("finger_ajax.php", { "id": topic_id },
function(data){
if(data ==="ok"){
alert("Thank you for your support!");
}else{
alert("Sorry, failed!");
}
}, "text");
//Get the current number of "likes" and add 1
var finger = parseInt($(".finger"+topic_id).html())+1;
// Update the number of "likes"
$(".finger"+topic_id).html(finger);
}

Database code (finger_ajax.sql):
Copy code The code is as follows:

DROP TABLE IF EXISTS `finger_ajax`;
CREATE TABLE `finger_ajax` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL DEFAULT '',
`finger` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

----------------------------------
-- Records of finger_ajax
-- ---- --------------------------
INSERT INTO `finger_ajax` VALUES ('1', 'The weather is not bad today! It’s good to do something ', '10');
INSERT INTO `finger_ajax` VALUES ('2', 'Welcome to www.jb51.net, the National Day is coming, I wish you all a happy National Day!!', '3') ;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/769247.htmlTechArticlephp is used more and more and more widely. In order to increase the richness of the website, there are many new technology. Ajax is an indispensable technology in modern websites. It can be asynchronous...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template