Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    65
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Query by multiple drop downs

    I am looking to add multiple dropdown menus to a progam for searching (Like the kind of searching you would find at any car dealership website) had have that search the Database. Here is the code so far with only one drop down list. Any advice on how I would rewrite this example to make it work?:

    PHP Code:
    <label>Make:

    <select name="make">
        <option value="Ford">Ford</option>
        <option value="Toyota">Toyota</option>
    </select>
    </label>

    <label>Model:

    <select name="model">
        <option value="Focus">Focus</option>
        <option value="Carola">Carola</option>
    </select>
    </label>



    <input type="submit" name="search" value="Search"/>

    </form>

    </center>
    <?php

    include 'includes/db/connect.php';

    if(isset(
    $_POST['search']) || isset($_POST['submit']))
    {     
        
    $make $_POST['make'];
        
    $model $_POST['model'];
        
        
    $query "SELECT * FROM veh WHERE $make & $model ORDER BY year ASC";

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Take a look at this ... PHP/MySQL/AJAX

    http://www.blueicestudios.com/chaine...hp-mysql-ajax/

    I've used it before and it's nice.
    To change the selections, you update the database table.

    I also like the user interface. AJAX makes it nice.

    Here is the 3-tier example:
    http://blueicestudios.com/chained-se...es/three-tier/

    I guess you can make as many tiers as you need.



    .

  • #3
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by mlseim View Post
    Take a look at this ... PHP/MySQL/AJAX

    http://www.blueicestudios.com/chaine...hp-mysql-ajax/

    I've used it before and it's nice.
    To change the selections, you update the database table.

    I also like the user interface. AJAX makes it nice.

    Here is the 3-tier example:
    http://blueicestudios.com/chained-se...es/three-tier/

    I guess you can make as many tiers as you need.

    .
    +1 for AJAX. Especially if your second and subsequential dropdowns are depending on the previous one (which I guess is the case, as Focus' are only made by Ford, so selecting a Toyota won't give that option?).

    If you want to program it yourself rather then using mlseims link (haven't checked it to be honest), have the <select> perform a JavaScript function on change or something.

    PHP Code:
    <select name="make" id="make" onchange="checkmodels()"
        <
    option value="Ford">Ford</option
        <
    option value="Toyota">Toyota</option
    </
    select
    Code:
    <script>
    function check_models() {
    var make = getElementById('make').value;
    // get AJAX request ready, bla bla bla
    // send AJAX request to functions.php?action=checkmodels
    // if AJAX response is good, change selectlist
    document.getElementById('models').innerHTML = ajax.responseText;
    Where the functions file loads the corresponding models based on the make
    PHP Code:
    switch($_GET['action'] {
    case 
    'checkmodels':
    $sql "SELECT * from models WHERE make = '{$_GET['action']}';
    /* perform query, load 1 return variable with the HTML you want outputted, so 
     * <select name="
    model"> 
     *   <option value="
    Focus">Focus</option> 
     *   <option value="
    Carola">Carola</option> 
     * </select> 
     */
    echo $return;
    break;

    Right, this is by no way a tutorial, nor a definitive answer, just an outline of an AJAX request to show you that it really isn't that hard

    Regards,
    Martin


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •