0

I'm trying to create a default value for my dynamic drop down list. After the user selects one of the options, they submit and that value is stored as a variable in the next page that I can access using $_POST['land']

I have created the same dynamic list in the next page and want that 'land' to appear first in the dynamic drop down menu. So far this is just the main code to show the dynamic drop down list. Any help would be appreciated. Thanks!

        while($row = mysqli_fetch_assoc($result))
                { 
                    extract ($row);
                    echo "<option value='$place'>$place</option>\n";

                }
1
  • you want the previously selected option to be first in the list? wouldn't it be enough to have it pre-selected, using <option selected="selected">$place</option>? Commented Feb 21, 2012 at 18:35

3 Answers 3

3

As far as I understand you want the item chosen on first page to appear as the default item in the second page.

Use this

while($row = mysqli_fetch_assoc($result))
{ 
    extract ($row);
    echo "<option value='$place'";
    echo ((isset($_POST['land']) && $_POST['land']==$place)?'selected="selected"':'');
    echo ">$place</option>\n";

}
Sign up to request clarification or add additional context in comments.

Comments

1
   while($row = mysqli_fetch_assoc($result))
            { 
                extract ($row);
                echo "<option value='$place' ";
                if($_POST['land'] == $place) {
                  echo "selected='selected'";
                }
                echo ">$place</option>\n";

            }

3 Comments

It's a little pedantic, but selected="selected" is better syntax to use.
@Grim... also, your username makes it read like I'm really hesitant to tell you anything :)
i did look this up on w3c and they said it's valid html5 to have just the attribute-name. but i'd suggest to have name and value, as well. =)
0
while($row = mysqli_fetch_assoc($result))
{ 
   extract ($row);
   $select = (isset($_POST['land']) && $_POST['land'] == $place)?"selected='selected'":"";
   echo "<option value='$place' $select >$place</option>\n";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.