Page 1 of 1

Dropdown reload SQL query

Posted: Thu Oct 14, 2010 10:36 am
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());
        }
    ?>

Re: Dropdown reload SQL query

Posted: Thu Oct 14, 2010 11:30 am
by Jonah Bron
Do you want the page to reload, or load the resorted data dynamically?

Re: Dropdown reload SQL query

Posted: Fri Oct 15, 2010 10:53 am
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.

Re: Dropdown reload SQL query

Posted: Fri Oct 15, 2010 12:06 pm
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>