I’m using the following PHP/Javascript code to create a drop down list. It’s part of a longer group of code which dynamically changes a second drop down list based on what’s chosen here. After the user submits their choice, I use PHP to grab relevant information, based on their choice, from my database. They’re going to be going through the process several times and it would be great if I could reselect whatever they just posted. Might there be a way to reselect whatever they $_POSTed the first time? If I could have some guidance for this piece of the code, I can hopefully figure out how to implement it in my 2nd drop down menu as well.
Thanks so much!
-Eric
$q1=mysql_query("select * from assignments_topics");
echo mysql_error();
$already_listed_chapters=array();
while($nt1=mysql_fetch_array($q1)){
if (!in_array($nt1['menu_chapter_name'],$already_listed_chapters)){
array_push($already_listed_chapters,$nt1['menu_chapter_name']);
echo "addOption(document.drop_list.Chapter, '$nt1[chapter_name]', '$nt1[menu_chapter_name]');";};
}// end of while
Thank you for the response but I’m aware of the isset function on the php side of things. I guess that I was hoping for some help with the javascript portion. Somewhere along the line:
I’ve got a drop down list named “Chapter”; each element in the list has a value “chapter_name”, while the printed value is “menu_chapter_name”. As my while code loops through, it goes to my database to see if the menu_chapter_name has been listed already; if not, then it prints it, but if it has, it won’t print it again.
tell the added option that it’s the currently selected one if it matches the posted option that was submitted.
The addOption function is what you yourself use to add an option, but that function is not a normal part of javascript.
You would need to add a boolean parameter to the addOption function that specified whether the option is selected or not, and that can only be done by knowing the code for the addOption function.