This article will introduce you to a jQuery + Ajax + PHP implementation quot; like quot; rating function code. when you click the red-hearted button on your favorite image on the page, the front-end page sends an ajax request to the background, and the background PHP program is connected... this article introduces 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 image information, including the name and path of the image and the total number of images "liked, pic_ip records the IP data that users click to like.
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