About jquery+php query mysql to implement the auto-complete function, I wrote this tutorial because most of the auto-complete applications I have seen just give you a program source code package and then tell you how to use it. Not telling you how it works and why it does it. And knowing this allows you to further customize this plug-in to your own needs (I have written many articles about other applications in my blog about this).
Okay, let’s get started now.
JavaScript code :
Copy to ClipboardQuoted content: [www.bkjia.com]Explanation of JS:
Okay, as you can see from the above code, we need to connect to a file called rpc.php, which handles all operations.
The lookup function uses the word obtained from the text input box and then passes it to rpc.php using the Ajax method POST in jQuery.
If the input character 'inputString' is '0' (Zero, translator's annotation: here refers to no input in the search box), the suggestion box will be hidden. This is also very user-friendly. If you want to search, Nothing is entered in the box, and you don't expect a suggestion box to appear.
If there is content in the input box, we get the ‘inputString’ and pass it to the rpc.php page, and then jQuery’s .post() function is used, as follows:
.post(url, [data], [callback])
The 'callback' part can be associated with a function. This is more interesting. It will only be executed when the data is loaded successfully (Annotation: This is a free translation, I did not understand the original text: <).
If the returned data is not empty (that is, there is something to display), then display the search prompt box and use the returned data to replace the html code in it.
It’s that simple!