Hi guys, i have a drop down code below


<select class="input" name="mod[cpu]">
<option value=""> </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

the output are like this

$html .= ($content[cpu] ? '<div class="' . $this->switch_bg() . '"><strong>CPU:</strong> ' . @htmlspecialchars($content[cpu]) . '</div>' : '');

i want after the drop down choosed by user, it will add SELECTED next time they opening the drop down again

i am still dont know how to put the SELECTED there, please help what is the code to add SELECTED, i only have variable mod[cpu] and $content[cpu]


i already tried in bold below, but still not working

<select class="input" name="mod[cpu]" selected="$content[cpu]">
<option value=""> </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

please share your knowledge

Dani AI

Generated

Your attempt put selected on the <select> element. That will not mark an option as chosen. The browser expects the selected boolean on an <option> element. To persist a user choice you re-render the control with the option that matches the saved/submitted value marked selected, or set the selection client-side with JavaScript.

Server-side pattern (PHP):

  • pick the source of truth: a submitted value (e.g. $_POST['mod']['cpu']) when redisplaying a form after validation, otherwise the saved value (e.g. $content['cpu']).
  • when outputting options, compare each option value to that chosen value and emit the selected attribute on the matching <option>.
  • always escape values with htmlspecialchars before echoing to avoid XSS.

Example (safe, minimal server-side render):

$selected = isset($content['cpu']) ? (string)$content['cpu'] : '';
$choices = ['', '1', '2', '3'];

echo '<select name="mod[cpu]" class="input">';
foreach ($choices as $c) {
    $attr = ($c === $selected) ? ' selected' : '';
    echo '<option value="' . htmlspecialchars($c, ENT_QUOTES, 'UTF-8') . '"' . $attr . '>'
         . htmlspecialchars($c, ENT_QUOTES, 'UTF-8') . '</option>';
}
echo '</select>';

Troubleshooting tips:

  • Inspect the generated HTML (View Source) to confirm which <option> has the attribute.
  • var_dump() the candidate value(s) to check type and whitespace. Use strict comparison (===) or normalize types.
  • Validate that the stored value exactly matches one of the option values.
  • Persisting across sessions requires saving the choice (as suggested); for immediate redisplay use $_POST first, then fall back to saved data.

For reference, see the HTML select element docs and PHP escaping guidance:

Thanks to and for pointing toward persistence and learning resources; the implementation detail above shows a simple, safe way to apply that advice.

Recommended Answers

All 2 Replies

I think you need to save users' choice in the database. And when you show it, you make the PHP or whatever you use read the data from database. If the value of the option is equal to the value in database, then

<option value="xxx" selected="selected">xxx</option>

.
Otherwise, just write

<option value="xxx"></option>

You can visit w3school.com for php tutorials.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.