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.

 

 

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.