Jump to content

How do you get a page to automatically re-select an option value in a form upon re-displaying?


zaniac

Recommended Posts

I'm trying to create a sticky form, but am having problems with the <select> tags. When the user makes an error, the form is displayed again. When this occurs, I am able to retain the strings in the various text areas, but am unable to get the <select> option chosen by the user to be re-selected automatically (enabling the user not requiring to choose the same option again.I am aware of the <selected="selected">, but this only works for one single option value within the whole <select> tag.Any help on this is greatly appreciated. TY;)

Link to comment
Share on other sites

The selected attribute is the one to use, what are you having problems with?
Basically, I am playing with the idea of making a page which both displays the form and validates it (using PHP). When there is a validation error I want the page to re-display the form and retain the results(i.e 'Sticky Form'). I am able to retain the inputs entered into the text boxes from the users original submit, but I cannot get the <select> tag to retain the option value which the user has made on the orginal submit. Lol, I hope that I'm not confusing you. Below is an example of the XHTML I currently have on my form:-------------------------------------------------------------------------------------------Line 1 <p>Country<select name="country" value="<?php $_POST['country'] ?>" >2 <option value="" > Choose Country</option>3 <option value="England" > England</option>4 <option value="Scotland"> Scotland</option>5 <option value="Wales"> Wales</option>6 <option value="N.Ireland"> Northern Ireland</option>7 </select>8 </p>-------------------------------------------------------------------------------------------Now I could add the selected attribute inside one of the option values in the <select> tag. However, I think that after submition whichever option value has the selected attribute, this will be the option which is re-displayed no matter what the user has chosen. Currently in the validation process, when there is an error this <select> tag automatically is reset to the "" option value and only text areas inputs are retained. For the above reason I am not sure if the selected attriubute could work. But if you think that it should then I am willing to give it another try. Thanks for any further help or advice you (or anyone else) can give. :)
Link to comment
Share on other sites

I do this quite often without any problems. If you are setting the value to empty string if it fails validation, then obviously you won't be able to tell which one was selected, so you'll need to retain the value and set the selected attribute on the matching option.

Link to comment
Share on other sites

Why don't you try

<p>Country<select name="country"><?php	$options = array("" => "Choose Country", "England" => " England", "Scotland" => " Scotland", "Wales" => " Wales", "N.Ireland" => " Northern Ireland");	if (isset($_POST['country'])) $selected = $_POST['country'];	else $selected = "";	foreach ($options as $value => $text) {		echo "<option value=\"$value\"";		if ($selected == $value) echo " selected";		echo ">$text</option>\n";	}?></select></p>

That way, the PHP code will append "selected" after the value that was sent by $_POST.Hope that helped :)

Link to comment
Share on other sites

Why don't you try
<p>Country<select name="country"><?php	$options = array("" => "Choose Country", "England" => " England", "Scotland" => " Scotland", "Wales" => " Wales", "N.Ireland" => " Northern Ireland");	if (isset($_POST['country'])) $selected = $_POST['country'];	else $selected = "";	foreach ($options as $value => $text) {		echo "<option value=\"$value\"";		if ($selected == $value) echo " selected";		echo ">$text</option>\n";	}?></select></p>

That way, the PHP code will append "selected" after the value that was sent by $_POST.Hope that helped :)

Hi Synook, thanks very much for the PHP tip, I'll give it a try. Many thanks :)
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...