Skip to content
Advertisement

Live data search using ajax. How to display another query when input is empty [closed]

I am trying to create a live search using ajax, jquery, php and mysql. The user enter some inputs, it send the search to form_livesearch.php. I got that part worked. Else if the input is empty, then display other query. (I need help with this part)

JavaScript

Here is the php and mysql I am trying to display when the input field is empty.

JavaScript

form_livesearch.php:

JavaScript

Advertisement

Answer

You should handle this stuff in the PHP file. and by the way, the input can not be empty as you put the ajax in keyup event. it just happened when the user use the backspace to delete what he search.

So the form_livesearch.php PHP file should be something like this.

JavaScript

You can use a separate file to handle 2 types but as they are all about products it’s better to have one file.

It’s a good practice to return the data and let the frontend build the HTML output but if you want to build HTML in the PHP file, it’s better to wrap them in a string.

Also, use the prepare statement of MySQLi to prevent SQL injection. take a look at this example for more information.

And the html file should be something like this:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement