JQuery + Ajax + PHP "like" rating function implementation code

Source: Internet
Author: User
Based on jQuery, this article implements a rating function through PHP and mysql, which is a simple and very good ajax application instance, you can refer to this article to introduce a jQuery + Ajax + PHP code for implementing the "like" rating function. when you click the red-hearted button on your favorite image on the page, the front-end page sends an ajax request to the backend. after receiving the request, the PHP program in the backend queries whether the user's click record exists in the IP address Library. if not, the corresponding value is + 1, at the same time, the user's IP information is written to the IP database, and vice versa, the user is "liked ".

Database Design
Prepare two tables. the pic table stores the image information, including the image name, path, and total number of "liked" images. pic_ip records the IP address data after the user clicks the favorite image.

CREATE TABLE IF NOT EXISTS `pic` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `pic_name` varchar(60) NOT NULL,  `pic_url` varchar(60) NOT NULL,  `love` int(11) NOT NULL DEFAULT '0',  PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

CREATE TABLE IF NOT EXISTS `pic_ip` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `pic_id` int(11) NOT NULL,  `ip` varchar(40) NOT NULL,  PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 

Index. php
In index. php, we use PHP to read and display the image information in the pic table, and use CSS to improve the page display effect.
The code is as follows:

 
 
 JQuery + Ajax + PHP achieves "like" rating
 

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.