Jump to content

sticky list box


dudleylearning
Go to solution Solved by requinix,

Recommended Posts

Hi,

 

how do I make the options within the list box sticky:

<label for="author">Author:</label>
<select name="author" id="author">
	<option value="">Select one</option>
	<?php foreach ($authors_in_db as $data): ?>
	<option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>">
		<?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?>
	</option>
	<?php endforeach; ?>
</select>

I've managed to do it with a text area:

<label for="joke_text">Type your joke here:</label>
<textarea id="joke_text" name="joke_text" rows="3"><?php if(isset($_POST['joke_text'])) { echo $_POST['joke_text']; }?></textarea>
Link to comment
Share on other sites

I've tried this:

<label for="author">Author:</label>
<select name="author" id="author">
	<option value="">Select one</option>
	<?php foreach ($authors_in_db as $data): ?>
	<option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>" selected>
		<?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?>
	</option>
	<?php endforeach; ?>
</select>

But that selects the last item. Would I need something similar to this:

<option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>"
<?php echo($data['name']?' selected="selected"':'');?>>
<?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?>
</option>

?

Link to comment
Share on other sites

Try it and find out :P

No really: a great thing about PHP is that if you're not sure about whether something will work or not then you can just try it and see what happens.

 

Though I'll say you should just do "selected" and not the whole "selected=selected" thing - it's more modern.

 

 

 

You have the right idea but there's a small problem with it.

>
Think about what that is saying: if $data[name] then echo selected else echo nothing. The echo parts are right but the condition isn't - it needs to be testing not whether the name has a value (which you know it does) but whether that value matches the one in the form.

 

 

Link to comment
Share on other sites

i've tried to take into account your point:

<label for="author">Author:</label>
<select name="author" id="author">
	<option value="">Select one</option>
	<?php foreach ($authors_in_db as $data): ?>
	<option value="<?php echo htmlspecialchars($data['id'], ENT_QUOTES, 'UTF-8'); ?>" 
		<?php if(isset($_POST[$data['id']]) && $_POST[$data['id']] == $data['id']) { echo($data['id']?' selected':'');} ?>>
		<?php echo htmlspecialchars($data['name'], ENT_QUOTES, 'UTF-8'); ?>
	</option>
	<?php endforeach; ?>
</select>

Surely I'm not that far off now? Anything that you can see that I am missing from it?

Link to comment
Share on other sites

  • Solution

Oh, I missed something that would have been nice to point out.

 

Remember how you made joke_text sticky? "If the joke_text is present in $_POST then output it"? Start with the same concept except with the author

if(isset($_POST['author'])) { echo $_POST['author']; }

then add a condition that the POSTed author matches the author in $data (remembering that what's in $_POST will be the author ID and not the name)

if(isset($_POST['author']) && $_POST['author'] == $data['id']) { echo $_POST['author']; }

then change the output to be not the value from $_POST but "selected".

if(isset($_POST['author']) && $_POST['author'] == $data['id']) { echo 'selected'; }

  • Like 1
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.