PHP search & select boxes

Discussion in 'PHP' started by AT-XE, Apr 21, 2008.

  1. #1
    Hello all,
    I was wondering if there is any way of searching in a database from drop-down boxes.

    I mean like this:

    [​IMG]

    and also if I can use the selection "any" for that.

    Please help I really need to know that.

    Thank you,
    -AT-XE
     
    AT-XE, Apr 21, 2008 IP
  2. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Pass the value of the drop down box, and compare it to the rows in your database. As simple as that :)
     
    Louis11, Apr 21, 2008 IP
  3. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried but I got some errors in the db.......
    Can you give me an example code?

    Thank you.
     
    AT-XE, Apr 21, 2008 IP
  4. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    + how to use the any selection?
     
    AT-XE, Apr 21, 2008 IP
  5. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #5
    
    <?
    $submit = $_POST['submit'];
    
    if($submit){
    // trying to submit the form
    $dropdown = $_POST['dropdown'];
    
    print "Drop down value is: " . $dropdown . "<br />";
    $query = NULL; // need this for the loop scope
    if($dropdown == "any"){
    $query = "select * from TABLE_NAME";
    }
    else{
    $query = "select * from TABLE_NAME where ROW='$dropdown'";
    }
    
    $sql = mysql_fetch_assoc($query);
    
    $row = mysql_fetch_assoc($sql){
    print $row['SOME_ROW'];
    }
    
    }
    else{
    // show the form
    ?>
    <form action="<? $_SERVER['php_self']; ?>" method="post">
    <select name="dropdown">
    <option value="any">Any</option>
    <option value="something">Something</option>
    <option value="blah">Blah</option>
    </select>
    <input type="submit" name="submit" value="Submit!" />
    </form>
    <?
    }
    ?>
    
    PHP:
    Haven't tested it, but that's the basic idea. It's part psuedo code part actual code, you just have to put it together (and work out any errors that I might have overlooked).
     
    Louis11, Apr 21, 2008 IP
  6. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you!
    One last question, what if I have more than one drop-downs?

    Thank you again!
    AT-XE
     
    AT-XE, Apr 22, 2008 IP
  7. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #7
    Say your second drop down is name "dropdown2", then in the php code you would have:

    
    $dropdown2 = $_POST['dropdown2'];
    
    // somewhere in an SQL statement
    $sql = "select * from TABLE_NAME where ROW='$dropdown1' and ROW='$dropdown2';
    
    mysql_query($sql) or die(mysql_error());
    
    PHP:
    Or something like that. Just basic passing of variables in PHP.
     
    Louis11, Apr 22, 2008 IP
  8. AT-XE

    AT-XE Peon

    Messages:
    676
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thank you :)
     
    AT-XE, Apr 22, 2008 IP