Page 1 of 1

Drop down menu changing SQL values in table

Posted: Sat May 01, 2010 8:59 pm
by nixes
Hi all,

I currently have a table on my webpage that is populated from a table in my SQL database. I'm looking to have a drop down menu above the table that has a list of values corresponding to the unique values in the first row of the table. When the user changes the value of this drop down box I want the table below to be updated to show the corresponding rows in the database.

i.e.
say i have a table like this in my database:
colour number
red 1
red 2
yellow 3
yellow 4
blue 5
blue 6

and I have a drop down box containing the three colours. When the user selects a colour from the drop down box I want the table below to be updated to show the two corresponding numbers.

Any help with this issue would be much appreciated.

Many thanks,
Nick

Re: Drop down menu changing SQL values in table

Posted: Sun May 02, 2010 11:43 am
by Jonah Bron
Do you mean without refreshing the page? Then you'll need Ajax. Use it to call a PHP page that outputs the new table. The PHP page will have a SQL query that looks something like this

Code: Select all

SELECT * FROM colors WHERE color = *color passed through ajax*
*color passed through ajax* is where you put in the value of, say, $_GET['color']. Your Ajax call will pass the GET variable "color" with the value the user selected.

A few tutorials that might help

http://www.w3schools.com/PHP/php_mysql_intro.asp
http://www.w3schools.com/Ajax/ajax_intro.asp

Re: Drop down menu changing SQL values in table

Posted: Sun May 02, 2010 5:43 pm
by nixes
A page refresh would be fine. As long as the table shows the new data (i.e. essentially, when the drop down box is changed from red to yellow, the page is refreshed and the query is changed to say SELECT x FROM y WHERE colour=yellow, instead of red)

I'm thinking there must be an easier solution than using AJAX? Essentially it seems as though its just a case of using a variable in the WHERE clause of the SELECT statement, and grabbing the value of that variable from the checkbox. I basically need to combine this with an onchange() style function to refresh the page and show the new table.

Cheers,
Nick