Jump to content

PHP Drop Down List from Database


Anfaenger

Recommended Posts

I am trying to do the following:

 

Create a drop down list from database values (see code below). When a value from the drop down list is selected, the corresponding data will be displayed in a container on the same page.

 

The drop down list consist of years (2010,2009,2008, etc.). For each year there are 12 values in the table (i.e. date=2009-01-31 value=1, 2009-02-28:2 and so on).

 

The drop down list works fine but the problem is the variable for the year in the container. I probably have to use some POST or GET to pass it on? Or must I use a <form> for the dropdownlist to pass on the value?

 

Thank you for any clues and hints.

 

//Create drop down list from DB
$result=mysql_query('select year from TABLE order by year desc');

echo '<select name="years" onchange="javascript:Show(\'abc\')">'
     	.'<option value="">Select Year</option>';
while($years = mysql_fetch_array($result)) {
echo '<option value="'.$years["year"].'">'.$years["year"].'</option>'; 

}

  	echo '</select><div id="abc" style="DISPLAY: none">';

$result=mysql_query('select date,value from TABLE where YEAR(date)="'.$years.'"');

	for ($i=1;$i<=12;$i++) {
list($date,$value)=mysql_fetch_row($result);
echo '<p>'.$date.': '.$value.'<p>';
}
echo '</div>';

Link to comment
https://forums.phpfreaks.com/topic/194882-php-drop-down-list-from-database/
Share on other sites

If you want it to happen as in an onclick event, your going to have to go the route of using some for of JavaScript magic to pull it off In this case seeing as your going to be working with php. I would suggest looking up AJAX and reading a bit on that. My personal favorite framework is jQuery and JQueryUI (an addon of sorts for jQuery)

 

But if your in a bind and you've no time to learn the extras. Your going to still need to use javascript I'd say on the form element itself in the "onClick" event add something like "this.FormName.submit" or something to that extent I dont think thats correct but thats the idea your looking for.

The onclick or onchange event is -I think- not the big problem. Whether it needs to be done in a form or without is not so relevant.

 

My problem is to query only the data selected in the drop down list. To achieve that I must pass on a variable and I cannot figure out how to do that. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.