Php uses ajax to implement the like, top, and step-on function instances

Source: Internet
Author: User

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 the like function in Weibo.

Features of this like:

Home page file (index. php ):
Copy codeThe Code is as follows:
<Script type = "text/javascript" src = "http://www.jb51.net/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']. "\ r \ n <a href = '# 'onclick = 'finger (". $ row ['id']. ") '> likes (<span class = 'finger ". $ row ['id']. "'> ". $ row ['finger ']. "</span>) </a> </p> \ r \ n ";
}
?>

Process ajax requests and configuration files (finger_ajax.php ):
Copy codeThe Code is as follows:
<? Php
/** The "like" 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'];
// Add "like" to 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 codeThe 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, it failed! ");
}
}, "Text ");
// Obtain the number of current "likes" and Add 1
Var finger = parseInt ($ (". finger" +topic_id).html () + 1;
// Update the number of likes
$ (". Finger" define topic_id).html (finger );
}

Database code (finger_ajax. SQL ):
Copy codeThe 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', 'Today's weather is good! What should I do? ', '10 ');
Insert into 'finger _ ajax 'VALUES ('2',' Welcome to www.jb51.net. The National Day is approaching. Wish you a happy National Day !! ', '3 ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.