Javascript/PHP: reselecting after $_POST

Hello!

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

<?php
if( isset( $_POST['whatever'] ) )
{
     // do something

     // do something with $_POST['whatever']
     echo $_POST['whatever'];
}

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:

echo "addOption(document.drop_list.Chapter, ‘$nt1[chapter_name]’, ‘$nt1[menu_chapter_name]’)

I need to not only add that particular option, but I need to select it if it was the $_POST option; I hope that my question makes more sense now.

-Eric

You can, but it depends on how the addOption function works. Can you provide more details on that particular part?

Sure!

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.

Does that make sense?

It makes sense, but it doesn’t help.

To achieve what you require takes two main steps.

  1. get from $_POST the option that was submitted
  2. 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.

My apologies Paul! :slight_smile:

function addOption(selectbox, value, text )
{
var optn = document.createElement(“OPTION”);
optn.text = text;
optn.value = value;

selectbox.options.add(optn);

}

Add another statement to that addOption function to set the selected property:


function addOption(selectbox, value, text, selected) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    optn.selected = selected;

    selectbox.options.add(optn);
}

Then in the PHP, you can work out whether it should be selected or not.


$selected = 'false';
if (...) {
    $selected = 'true';
}
echo "addOption(..., '...', '...', $selected);";

Now all you have to do is to work out how to tell if the option is the same as the posted one.

Clear as a bell. Thank you and enjoy your weekend.

-Eric