Problem with a search form

Discussion in 'PHP' started by Amilo, Oct 23, 2006.

  1. #1
    I have a form set up on a webpage and when data is entered the database returns results with no problems.

    The problem I have is if a user leaves the form box empty and hits enter then the whole database of results is diplayed.

    How can I give an error message if nothing is entered in the search box ?

    <form action="/results.php" method="post">
                                  <div align="center">
                                    <input type="text" name="search">
                                    <br>
                                    <input type="submit">
                                  </div>
                                </form>
    Code (markup):
     
    Amilo, Oct 23, 2006 IP
  2. Fl1p

    Fl1p Active Member

    Messages:
    511
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I think the code below would work, basically it will check for the blank field and display a message. You should name your "Submit" input so you can check if the user hit the "Submit" button or not and results.php will only be executed if the user use the "Submit" button.

     
    Fl1p, Oct 23, 2006 IP
  3. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Fl1p, I will report back if it works or not.;)
     
    Amilo, Oct 23, 2006 IP
  4. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am getting a blank white page ?
    I should be able to solve this but its very late.
     
    Amilo, Oct 23, 2006 IP
  5. TheGuy

    TheGuy Peon

    Messages:
    138
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What did you put instead of "//Your command goes here"?
     
    TheGuy, Oct 23, 2006 IP
  6. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #6
    <form action="/results.php" method="post">
    <div align="center">
    <input type="text" name="search"><br/>
    <input type="submit" name="submit">
    </div>
    </form>

    <?php

    $search = $_POST['search'];

    if ($_POST['submit']) {

    if (empty($search) ){

    echo " Your left this field blank ";
    }else {
    //do something here
    }
    }

    ?>
     
    php-lover, Oct 23, 2006 IP
  7. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #7
    Should be placed on top, not bottom:

    
    if($_POST['submit'] == NULL){
       if($_POST['search'] == NULL){$message = 'Please enter search term.';}
       if($message == NULL){
    //Do  your work
       }
    }
    //Display message anywhere you want on screeen
    
    PHP:
    Peace,
     
    Barti1987, Oct 24, 2006 IP