Home > Backend Development > PHP Tutorial > php ajax.$post realizes the functions of saving, liking and disliking_PHP tutorial

php ajax.$post realizes the functions of saving, liking and disliking_PHP tutorial

WBOY
Release: 2016-07-13 10:44:19
Original
909 people have browsed it

The save, like, and dislike functions are mainly used in comments. Here we will make a simple mysql implementation based on jquery ajax and php. The source code download is attached at the end of the article.

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 saving function in Weibo, all using this.

Rendering of this saving function:

Homepage file (index.php):

The code is as follows Copy code
 代码如下 复制代码

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 "

".$row['title']." 攒一下(".$row['finger'].")

";

 }

?>


代码如下 复制代码

/**"攒" 功能 响应ajax请求*/

//配置

$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


//接受对应的id

if(!empty($_POST['id'])){

$id = $_POST['id'];

//“攒”加1

$sql = "update finger_ajax set finger=finger+1 where id=$id;";

if(mysql_query($sql,$link)){

echo "ok";

}else{

echo "failed";

}

}

?>

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 "

".$row['title']." < img src='finger.jpg'/>Save it (".$row['finger']."

"; } ?> Processing ajax requests and configuration information file (finger_ajax.php):
The code is as follows Copy code
<🎜> /**"Save" 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'];<🎜> <🎜> //"Save" plus 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):

//Save js

The code is as follows Copy code
 代码如下 复制代码

function finger(topic_id){

 $.post("finger_ajax.php", { "id": topic_id },

   function(data){

     if(data=="ok"){

   alert("感谢您的支持!");

  }else{

   alert("对不起,失败了!");

  }

   }, "text"); 

 //获取当前“攒”的次数并加1

 var finger = parseInt($(".finger"+topic_id).html())+1;

 //更新“攒”的次数

 $(".finger"+topic_id).html(finger);

}

function finger(topic_id){


$.post("finger_ajax.php", { "id": topic_id },

 代码如下 复制代码


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', '今天天气还不错哦!去做点什么好呢?', '10');

INSERT INTO `finger_ajax` VALUES ('2', '欢迎来到 www.bKjia.c0m,国庆将至,祝大家国庆节快乐!!', '3');

function(data){


if(data=="ok"){

alert("Thank you for your support!");

}else{

alert("Sorry, failed!"); } }, "text"); //Get the current number of "save" and add 1 var finger = parseInt($(".finger"+topic_id).html())+1; //Update the number of "save" times $(".finger"+topic_id).html(finger);
}
Database code (finger_ajax.sql):
The code is as follows Copy code
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! What should we do?', '10'); INSERT INTO `finger_ajax` VALUES ('2', 'Welcome to www.bKjia.c0m, the National Day is approaching, I wish everyone a happy National Day!!', '3');
Original address: php ajax implements the functions of save, like and dislike http://file.bKjia.c0m/upload/2013/12/a63.zip http://www.bkjia.com/PHPjc/633118.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633118.htmlTechArticleThe save, like and dislike functions are mainly used in comments. Here we will make a simple mysql based on jquery ajax and php. To implement the save, like, and dislike functions, the source code download is attached at the end of the article. There are more and more php applications...
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