php - How to get result from database on the change of values in text box using Ajax? -


I am working in PHP and MySQL combo

I have to search for results from DB a hidden Regarding the change in the value of the user inserts in the field, a text box

How can I use Ajax?

You'll need a few things:

  • Your home page < / Li>
  • A PHP file that processes the given input (search string) and only shows the field that you want to store in hidden fields (returnsearchresults.php in the example below).
  • Some JavaScript to work Ajax

I advise to use jQuery for Ajax, although you are a masochist, you can always roll yourself.

On your main page you will need something like this:

  $ ("# ID_OF_SEARCH_BOX_GOES_HERE") keydown (task (event) {search_string = $ ("# ID_OF_SEARCH_BOX_GOES_HERE "). Val (); $ (" #ID_OF_HIDDEN_FIELD_GOES_HERE "). Load (" returnsearchresults.php? Q = "+ search_string);}); Edit  

Edit: Here's an idea whether returnsearchresults.php should look like this:

  & lt ;? Php $ search_string = $ _GET ['q']; // Add some checks to prevent SQL injection or completely $ SQL =, which is from your database, from which '% $ q%' "; $ result = mysql_query ($ sql); like 'column'; ($ Line = mysql_fetch_array ($ result)) {echo $ line [0], ','; // This line depends on the fact that you need to format your hidden area. }? & Gt;  

Note: I used to open / close the database connection or $ _GET ['q'] Napuarn no it did not include the code to verify. I'll leave it up to you!


Comments