-3

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!

Rajendran Nadar
  • 4,962
  • 3
  • 30
  • 51
  • Show us some code.. How does it look, what have you done so far? What are you expecting, what's failing? – Naruto Dec 04 '15 at 14:09
  • Well server side you can do `!empty`, to confirm the input isn't empty; you can use http://php.net/manual/en/function.strlen.php to check for a minimum length.. – chris85 Dec 04 '15 at 14:16

2 Answers2

1

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

<input type = "text" name = "yoursearchbox" required>
Manoj Salvi
  • 2,639
  • 1
  • 17
  • 21
  • i know there a lot of solutions to get this done using client-side javascript or through other scripting languages like php but that would require a deep understanding of various functions to achieve that so i put the simplest.. – Manoj Salvi Dec 04 '15 at 14:20
  • wow worked and that was so simple :) But can i put custom error message if no query entered. – Rajendran Nadar Dec 04 '15 at 14:28
  • Yes you can put custom error messages but you need to learn JavaScript or php or any other language of your choice to achieve that.. go for some online tutorials.. – Manoj Salvi Dec 04 '15 at 14:46
0

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
    }
Ben
  • 8,894
  • 7
  • 44
  • 80