Home >Backend Development >PHP Tutorial >Example of using php combined with ajax to realize like, like and dislike functions_PHP tutorial
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:
Homepage file (index.php):
$sql = "select * from finger_ajax";
$res = mysql_query($sql,$link);
while($row = mysql_fetch_array($res)){
echo "< p>".$row['title']." rnLike(".$row['finger'].")
Processing ajax requests and configuration information files (finger_ajax.php):
$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):
----------------------------------
-- 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') ;