Php search box prompt (automatically completed) instance code _ PHP Tutorial

Source: Internet
Author: User
The php search box prompts (automatically completed) the instance code. Baidu's search is in use. when users enter text, the search box automatically prompts related information, enhancing the user experience, how can this effect be achieved? Baidu's search is in use. when a user inputs text, the search box automatically prompts related information, enhancing the user experience, how can this effect be achieved?

Let's take a look. this is more dynamic. Otherwise, you still don't know what I'm talking about and what kind of effect I want to achieve!

The following describes the principles:

On the search.html page, when you enter "j" in the search box, use javascript to retrieve the text content in the search box, search for the relevant content in the database, and return the content, then, use javascript to display the results returned by the server in the prompt box under the search box for your reference.

Specific Implementation methods:

First, complete the search box, search button, and display the search prompt layer on the page, as shown in the following code:




View the page in a browser.

It looks very common and there are no Styles. now I want to adjust the styles.

# Search {font-size: 14px ;}
# Search. k {padding: 2px 1px; width: 320px;}/* set the search box width to a greater value. WEB development notes (www.chhua.com )*/

Adjust the style of the layer that displays the search prompt. because the search prompt layer is at the bottom of the search box, we set the positioning method to absolute positioning.

# Search_auto {border: 1px solid #817FB2; position: absolute;}/* set the border and positioning mode */

Then, use JS to place the position of the search prompt layer at the bottom of the search box, and the width is the same as that of the search box. here we use jQuery to solve this problem.

Detail ('elasticsearch_auto'detail .css ({'width': $ ('# search input [name = "k"]'). width () + 4 });

The position and width of the search prompt layer have been determined. because the prompt box is not displayed before you enter the search text, you must first hide it, add display: none in the style of the prompt layer to hide it.

Now we only need to register the event for the onkeyup in the search box. we still use jQuery to handle the event. in jQuery, the keyup

$ ('# Search input [name = "k"]'). keyup (function (){
$. Post ('search _ auto. php ', {'value': $ (this ). val ()}, function (data) {// send post data to search_auto.php on the server, $. post is the jQuery method.
If (data = '0') then ('{search_auto'{.html(''{.css ('display', 'none'); // determines the data returned on the server. if it is equal to 0, no relevant content is found, so clear the content of the prompt box and hide the WEB development notes (www.chhua.com )*/
Else response ('{search_auto'{.html(data=.css ('display', 'block'); // if the data returned on the server is not equal to 0, the returned content is placed in the prompt box and a prompt box is displayed.
});
});

The above client is ready, and you can send user input to the server and respond to the server's return value.

So how does the server process the data sent by the client? the following uses PHP as an example.

$ V = $ _ POST [value];
$ Re = mysql_query ("select * from test where title like '% $ v %' order by addtime desc limit 10"); // data sent from the client, query 10 related results in the database
If (mysql_num_rows ($ re) <= 0) exit ('0'); // judge the query result. if no relevant result exists, 0 is returned directly.
Echo'

    ';
    While ($ ro = mysql_fetch_array ($ re) echo'
  • '. $ Ro [title].'
  • '; // Output the title of the result returned by the query, which should be noted in this place, because the text returned by ajax technology through jQuery is UTF-8 encoding, so if $ ro [title] contains Chinese characters, remember to use PHP's iconv or other functions to convert it to UTF-8 encoding, otherwise you will see a garbled string on the page
    Echo'
  • Close'; // Enter a close button so that you can click close when you do not want to see the prompt layer. the close button uses jQuery. to explain, the current button is $ (this ), keep searching for its third parent element, so that it gradually disappears from WEB development notes (www.chhua.com )*/
    Echo'
';
?>

Now that the server can correctly execute the data we sent and return the corresponding results, enter a text in the search box to test it, the premise is that your database contains content related to this text. Otherwise, you cannot see the prompt box, because there is no relevant prompt content.

However, there are still some shortcomings, that is, the content in the prompt box is not beautiful. compared with the prompt box we saw in Baidu search, it is really ugly. haha, don't worry, we use css to adjust the display effect.

# Search_auto li {background: # FFF; text-align: left;}/* Set the li label effect in the prompt box */
# Search_auto li. cls {text-align: right;}/* Set the closing Button effect in the prompt box */
# Search_auto li a {display: block; padding: 5px 6px; cursor: pointer; color: #666 ;}/ * Set the effect of a label under the li label in the prompt box */
# Search_auto li a: hover {background: # D8D8D8; text-decoration: none; color: #000 ;}/ * effects of moving the cursor into the prompt box */

Now it is truly complete. if you want to set a latency or other better functions, you can leave it to your friends. you can also reply to your thoughts below, and so on.

Complete client code:




Jquery + php automatically prompts users to enter the search content







Related Article

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.