Drop down menu changing SQL values in table

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
nixes
Forum Newbie
Posts: 4
Joined: Wed Apr 07, 2010 8:33 am

Drop down menu changing SQL values in table

Post 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
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Drop down menu changing SQL values in table

Post 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
nixes
Forum Newbie
Posts: 4
Joined: Wed Apr 07, 2010 8:33 am

Re: Drop down menu changing SQL values in table

Post 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
Post Reply