Jump to content

[SOLVED] retaining <select> form value after refresh


unsider

Recommended Posts

I've alowed my users to "preview" the article their creating, check that the WYM editor is functioning properly, but in order to place the article into a category I'm using a <select> form, and the page is refreshed upon previewing the creation, and the select form value is reset.

 

So I'm curious, is it possible to store the selected option in a session value, or would this involve using JS client side to hold the value. Maybe a mixture of both?

 

Thanks.

 

 

Let's say you're using POST to send the select data to the verification page.

 

What I do is (this is if the 'values' in your <options> are integers, but you could work around that if you're doing something else):

 

$selected = array();
for ($i = 0; $i < $numberofvaluesinselect; $i++) {
if ($i == $_POST['selectname'])
	$selected[$i] = ' selected';
else
	$selected[$i] = '';
}

// Stuff happens, we close PHP with ?> and we're printing the form again:

<select name="bla">
<option value="0" <?php echo $selected[0]; ?>>Option 0</option>
<option value="1" <?php echo $selected[1]; ?>>Option 1</option>
<option value="2" <?php echo $selected[2]; ?>>Option 2</option>
<!-- etc -->
</select>

 

So, if the value was selected from last time, the <option> of that value says 'selected', otherwise nothing.

 

 

 

 

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.