Dropdown reload SQL query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
foxtamerind
Forum Newbie
Posts: 3
Joined: Mon Sep 20, 2010 9:46 am

Dropdown reload SQL query

Post by foxtamerind »

I need to switch SQL queries when I select an option from a dropdown menu using PHP without using a submit button. How would I do so with javaScript? I have nothing in my <head> to support this so far.

Code: Select all

<div class="main-content">
    <h1>Matches &nbsp;
        <form method="get">
        <select id="match_order" name="match_order">
          <option selected="selected" disabled="disabled">Select an Order</option>
          <option value="1">Big</option>
          <option value="2">Little</option>
          <option value="3">Year Matched</option>
        </select>
        </form>
    </h1>
    <?php 
        switch ($match_order) {
            case 1: 
                $match_set = mysql_query('SELECT * FROM matches ORDER BY big ASC', $connection);
                break;
            case 2:
                $match_set = mysql_query('SELECT * FROM matches ORDER BY little ASC', $connection);
                break;
            case 3:
                $match_set = mysql_query('SELECT * FROM matches ORDER BY match_year ASC', $connection);
                break;
            default:
                $match_set = mysql_query('SELECT * FROM matches ORDER BY big ASC', $connection);
                break;
        }
        

        if (!$match_set) {
            die("Database query failed: " . mysql_error());
        }
    ?>
Last edited by Benjamin on Fri Oct 15, 2010 10:56 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Dropdown reload SQL query

Post by Jonah Bron »

Do you want the page to reload, or load the resorted data dynamically?
foxtamerind
Forum Newbie
Posts: 3
Joined: Mon Sep 20, 2010 9:46 am

Re: Dropdown reload SQL query

Post by foxtamerind »

I'd either way, I just want it to sort when an option is selected from the drop down without having to use a submit button.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Dropdown reload SQL query

Post by Jonah Bron »

Easy. Here's an example:

Code: Select all

<select onchange="window.location = 'http://example.com/page.php?sortby=' + this.value;">
    <option value="">Order by...</select>
    <option value="date">Date</option>
    <option value="name">Name</option>
    <option value="price">Price</option>
</select>
Post Reply