#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2018
    Posts
    8
    Rep Power
    0

    Search Form Drop Down - How can I select all?


    Hello all,

    I've created a simple search form with 2 fields and a search button. Fields are 'grade' and 'office-location'.

    It is connected to a database table (staff) populated with the details of company employees.

    The user can therefore extract from the database company employees based on their grade and office location.

    My simple code for this is:

    PHP Code:
    <?php

    $grade 
    $_POST['grade'];
    $office_location $_POST['office_location'];

    $sql "SELECT * FROM staff WHERE grade = '".$grade."' AND office_location = '".$office_location."'";
    $result =mysqli_query($db_connection$sql);


    While (
    $rows mysqli_fetch_array($result)){
    echo 
    $rows['firstname'];
    echo 
    $rows['grade'];
    echo 
    $rows['office_location'];
    }

    ?>

    However, I want to be able to have an option in my form to select from all office_locations and staff of all grades.
    I thought this could be done in the form with the following line in the drop down menu:
    Code:
    <option value="" selected="selected">All locations</option>
    - with "" selected as the value.

    However, this doesn't work for me - it seems to return no values when this option is selected.

    This seems like it should be something simple, but not simple enough for me sadly....

    If anybody has any ideas that would be great.

    Thanks!
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,820
    Rep Power
    9646
    Try
    Code:
    <select name="office_location[]" size="5" multiple>
    Then you have to fix your code so that it can handle $office_location being an array...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2018
    Posts
    8
    Rep Power
    0
    Originally Posted by requinix
    Try
    Code:
    <select name="office_location[]" size="5" multiple>
    Then you have to fix your code so that it can handle $office_location being an array...
    Many thanks Requinix,

    Would that allow me to then select multiple locations, or would selecting 'All locations' as the first option in the drop down list just
    select everything in the array?

    Cheers.
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,820
    Rep Power
    9646
    Ah, I think I misunderstood.

    If you want a way to select everything and it's not that you want a way to select more than one (but not necessarily all) then doing
    Code:
    <option value="" selected="selected">All locations</option>
    is fine but you have to make your code recognize if that option was selected and, if so, not do the
    Code:
    AND office_location = '".$office_location."'
    part of the query. Because if you have that part then you'll be executing
    Code:
    AND office_location = ''
    which clearly won't work.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2018
    Posts
    8
    Rep Power
    0
    Ah okay - great. Thanks for the quick response.

    I'll have a think about getting my code to recognize if nothing was selected and hopefully come up with a workable solution.

    Many thanks for your help.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2018
    Posts
    8
    Rep Power
    0

    After several failed attempts...


    Originally Posted by confused88
    Ah okay - great. Thanks for the quick response.

    I'll have a think about getting my code to recognize if nothing was selected and hopefully come up with a workable solution.

    Many thanks for your help.
    Hello again,

    I had a think about your comment and put the following on my results form:

    PHP Code:
    $office_location   = isset($_POST['office_location'  ]) ? $_POST['office_location'  ] : 'Boston'.'<br>'.'Paris'.'<br>'.'Delhi'
    My thinking was that if no value was entered for office location in the POST form, then it would be assigned the locations of Boston, Paris and Delhi (assuming these are the only three office locations)

    I tried this and....well it didn't work. It just returned no values again.

    Is the above a legitimate way of assigning values to a string? Should something like this work?

    Many thanks for your time requinix.
  12. #7
  13. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,820
    Rep Power
    9646
    With a regular <select> there is no way to not pick an option. There will always be a value in there according to whatever <option> was selected, be that by default or by the user.

    So keep the code you had before and
    PHP Code:
    if ($office_location == "") { 
    Then read what I said about adjusting your query - because that <br> stuff you invented there will not work.

IMN logo majestic logo threadwatch logo seochat tools logo