Recently, xiaofeng and xiaofeng are implementing a Digg-like aggregation website. One of the most common features is the "TOP" post. Although this function can be achieved through common hyperconnections, however, there will be a refresh process after a user clicks. To have a better experience, it is necessary to use AJAX, which is also a typical AJAX application. AJAX is used between user operations and servers.
Recently, xiaofeng and xiaofeng are implementing a Digg-like aggregation website. One of the most common features is the "TOP" post. Although this function can be achieved through common hyperconnections, however, there will be a refresh process after a user clicks. To have a better experience, it is necessary to use AJAX, which is also a typical AJAX application. AJAX is used between user operations and servers.
Recently, xiaofeng and xiaofeng are implementing a Digg-like aggregation website. One of the most common features is the "TOP" post. Although this function can be achieved through common hyperconnections, however, there will be a refresh process after a user clicks. To have a better experience, it is necessary to use AJAX, which is also a typical AJAX application.
AJAX builds a bridge between user operations and servers, changing the disadvantages of refreshing the page each time you click in a browser to obtain new data, the user's behavior in the browser will be captured by the AJAX JavaScript script, and the data will be retrieved from the server in a timely manner, and then displayed in the browser.
1. Implementation functions and structure
The final result is to implement a hyper-connection and a span. Click the hyper-connection button to add 1 to the number of times this article is topped in the database, in addition, add 1 to the top number displayed in the span. For details, refer to the Digg button on the Digg.com homepage. This feature is implemented in the following two files:
-
Index. php: list of articles. There are multiple articles. The top number and "TOP" button of the article are displayed on the left of the title of each article;
-
Dig. php: accept a get parameter named postid and the value is the Document id. The function is to add 1 to the number of times this article is stored in the database;
- Dig. js: stores the Click Event of the hyperlink. After the hyperlink is clicked, the article id is sent to dig. php, and the top number is added to the index. php page;
2. "TOP" and number of times of being topped in index. php
An article list contains multiple articles. To control the number of times of each article, you must specify a unique id for the span that displays the number of top times. For example, the following code can implement this function ($ row is the row taken from the database, where id is the Article id and dig is the number of times the article is topped ):
Using the article's id in the database and a fixed string, it specifies a unique id for span, such as a numDig-474.
There is also a superjoin for clicking at the bottom. The superjoin click event calls the self-defined dig function in javascript and sends a parameter to it. The parameter content is the id of the topic, the implementation is as follows:
1
|
) "Id =" btnDig-
"> Top
|
In the article list, the number of top-mounted connections is displayed.
2. Implementation of AJAX in dig. js
For this script, it must implement the following two functions:
- Change the number of times of being topped in the span with the id numDig-postid in index. php to 1;
- Submit the id of the top article to dig. php to update the data in the database;
First, let's look at how to change the display of top times in index. php. Here we use a custom function. The function name is changeNumDig. We need to transmit a parameter, that is, the id of the document that is changed to the top times. After the function is called, check whether the parameter is a number. If yes, select the id of the span that shows the number of top times. Its structure is numDig-postid, then obtain the stored top value and increase by 1. The Code is as follows:
1 2 3 4 5 6 7 8 9 10
|
function changeNumDig(postid) { if(!isNaN(postid)) { spanNumDigName = "numDig-" + postid; numDig = document.getElementById(spanNumDigName).innerHTML; if(!isNaN(numDig)) { numDig ++; document.getElementById(spanNumDigName).innerHTML = numDig; } } }
|
In addition, you can change the number of times the database is topped, that is, submit the id of the top article to dig. php, construct a URL, and then submit it. In this process, we need to use XmlHttpRequest, the most important object in AJAX. Different browsers are created for this object. For details about how to create this object, see W3CSchool. The Code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
function GetXmlHttpRequest() { var xmlHttpReq = null; try { xmlHttpReq = new XMLHttpRequest(); } catch(e) { try { xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttpReq; }
|
The specific implementation process is to use the open method of the XmlHttpRequest object. The implementation code is as follows:
1 2 3 4 5 6 7 8 9
|
function dig(postid) { if(!isNaN(postid)) { var url = "dig.php?postid=" + postid; xmlHttpReq = GetXmlHttpRequest(); xmlHttpReq.open("GET", url, true); xmlHttpReq.send(null); changeNumDig(postid); } }
|
3. dig. php modifies the number of times the database is topped.
Dig. php accepts an article id that needs to change the number of times it is topped, queries its row in the database, and adds its dig attribute to 1.
In consideration of security issues, you also need to check whether the passed Article id is a number, connect to the database, construct a query string, and run the check and close command. The specific implementation code is as follows:
1 2 3 4 5 6 7 8 9 10
|
if(!empty($_GET['postid']) and (ctype_digit($_GET['postid']))) { require_once('config.php'); $db = new mysqli($dbhost, $dbuser, $dbpwd, $dbname) or die(); $query_string = "UPDATE top10_posts SET dig = dig + 1 WHERE id ={$_GET['postid']}"; $db->query('SET character_set_client = utf8'); $db->query('SET character_set_connection = utf8'); $db->query('SET character_set_results = utf8'); $db->query($query_string); $db->close(); }
|
The above three parts can be used to implement the top-up function, which can be seen in many web and aggregation websites, but it may be relatively complicated, but the implementation logic is mostly the same.
Recently, xiaofeng and xiaofeng are implementing a Digg-like aggregation website. One of the most common features is the "TOP" post. Although this function can be achieved through common hyperconnections, however, there will be a refresh process after a user clicks. To have a better experience, it is necessary to use AJAX, which is also a typical AJAX application.
AJAX builds a bridge between user operations and servers, changing the disadvantages of refreshing the page each time you click in a browser to obtain new data, the user's behavior in the browser will be captured by the AJAX JavaScript script, and the data will be retrieved from the server in a timely manner, and then displayed in the browser.
1. Implementation functions and structure
The final result is to implement a hyper-connection and a span. Click the hyper-connection button to add 1 to the number of times this article is topped in the database, in addition, add 1 to the top number displayed in the span. For details, refer to the Digg button on the Digg.com homepage. This feature is implemented in the following two files:
-
Index. php: list of articles. There are multiple articles. The top number and "TOP" button of the article are displayed on the left of the title of each article;
-
Dig. php: accept a get parameter named postid and the value is the Document id. The function is to add 1 to the number of times this article is stored in the database;
- Dig. js: stores the Click Event of the hyperlink. After the hyperlink is clicked, the article id is sent to dig. php, and the top number is added to the index. php page;
Original article address: using PHP and AJAX to implement the "TOP" post function, thanks to the original author for sharing.