Check if search query is entered or not

250 Views Asked by At

I made a search bar for my website with bootstrap everything is fine now the problem is that it can also be used when nothing is entered.

So how can i check for that with php and also add a min letter to search.

Just give me a rough idea that what I can do!

2

There are 2 best solutions below

3
On BEST ANSWER

well the simplest solution is add required to it for eg.

<input type = "text" name = "yoursearchbox" required>
0
On

If you are using HTML5 you can use required:

<input type="text" name="search" required>

In PHP, you can validate in a number of ways, like so:

<?php
    if(!isset($_POST["search"])        //The value is not set
        ||empty(@$_POST["search"])     //The value is empty
        ||strlen(@$_POST["search"])<1) //The string is shorter than 1 character
    {
        //Search is empty
    }