Click "save", "TOP", and "step on" in the comments. Here we will make a simple implementation of mysql based on jquery ajax and php, and click "TOP" and "step on". The source code download is provided at the end of the article.
Php applications are becoming more and more widely used. Many new technologies have emerged to increase the diversity of websites. Ajax is an indispensable technology in modern websites. It can be used to asynchronously refresh data and achieve many effects, such as refreshing verification codes and saving functions in Weibo.
This saving feature:
Home page file (index. php ):
| The Code is as follows: |
Copy code |
|
<Script type = "text/javascript" src = "/Public/js/jquery. min. js"> </script>
<Script type = "text/javascript" src = "finger_ajax.js"> </script>
<? Php
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']. "<a href = '# 'onclick = 'finger (". $ row ['id']. ") '> Save (<span class = 'finger ". $ row ['id']. "'> ". $ row ['finger ']. "</span>) </a> </p> ";
}
?> |
Process ajax requests and configuration files (finger_ajax.php ):
| The Code is as follows: |
Copy code |
|
<? Php
/** The "save" function responds to ajax requests */
// Configure
$ 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" and Add 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 ):
// Collect 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 ("thank you for your support! ");
} Else {
Alert ("Sorry, it failed! ");
}
}, "Text ");
// Obtain the number of current "saves" and Add 1
Var finger = parseInt ($ (". finger" +topic_id).html () + 1;
// Update the number of "Collected" Times
$ (". Finger" define 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', 'Today's weather is good! What should I do? ', '10 ');
Insert into 'finger _ ajax 'VALUES ('2',' Welcome to www. bKjia. c0m. The National Day is approaching. Wish you a happy National Day !! ', '3 '); |
Php ajax implementation, top, step on the function of http://file.bKjia. c0m/upload/2013/12/a63.zip