I have a html select list where one can chose an option.

<select name='price' id='price'>
                <option value='' >Select....</option>
                <option value='0-50,000'>0-50,000</option>
                <option value='50,000-100,000'>50,000-100,000</option>
                <option value='100,000-150,000'>100,000-150,000</option>
                <option value='150,000-200,000'>150,000-200,000</option>
                <option value='200,000 and above'>200,000 and above</option>
                <option value='see all'>See All</option>
</select>

When this list is submitted via a html submit button, this list shows again in another page. Now, I want the option the user selected to be new selected value. I am doing this:

<select name='price' id='price'>
                <option value='{$_POST['price']}'>{$_POST['price']}</option>
                <option value='0-50,000'>0-50,000</option>
                <option value='50,000-100,000'>50,000-100,000</option>
                <option value='100,000-150,000'>100,000-150,000</option>
                <option value='150,000-200,000'>150,000-200,000</option>
                <option value='200,000 and above'>200,000 and above</option>
                <option value='see all'>See All</option>
 </select>

But values are appearing twice. The option the user selected is shown as the selected and still appears in the list. For example, we now have something like this:

0-50,000 (this is the selected value)
0-50,000
50,000-100,000
100,000-150,000
150,000-200,000
200,000 and above

How do I solve this?

Dani AI

Generated

Brief diagnosis: the duplicate option appears because the submitted value was inserted as a new <option> instead of marking the matching existing option as selected. pointed this out, and offered a per-option PHP ternary solution that will work, but it is verbose and his example relies on short PHP echo tags (which can be disabled on some servers). A cleaner, safer pattern is to render the option list from a single canonical source and set the selected attribute only when the value matches.

A concise, maintainable server-side approach is to keep the allowed options in an array, trim the incoming value, then loop to emit each option while escaping output. This avoids duplicates and centralizes the list so additions or label changes only happen in one place.

<?php
$prices = [
  '' => 'Select....',
  '0-50,000' => '0-50,000',
  '50,000-100,000' => '50,000-100,000',
  '100,000-150,000' => '100,000-150,000',
  '150,000-200,000' => '150,000-200,000',
  '200,000 and above' => '200,000 and above',
  'see all' => 'See All',
];

$selected = isset($_POST['price']) ? trim($_POST['price']) : '';

foreach ($prices as $value => $label) {
    $sel = ($value === $selected) ? ' selected' : '';
    printf(
        '<option value="%s"%s>%s</option>' . "\n",
        htmlspecialchars($value, ENT_QUOTES),
        $sel,
        htmlspecialchars($label)
    );
}
?>

Extra tips and pitfalls: ensure exact string matches (trim and use strict comparison), escape all echoed values with htmlspecialchars to prevent XSS, and prefer full <?php ?> tags if short tags are disabled. If preserving an unknown submitted value is desired, add it only when it is not already in the canonical array (append it and mark selected) instead of duplicating an existing option. A lightweight client-side alternative is to set select.value with the saved value after the page loads, but server-side rendering from one source is usually the clearest and most reliable solution.

Recommended Answers

All 3 Replies

For the second page, remove the <option value='{$_POST['price']}'>{$_POST['price']}</option>, then in each of the option, add an if statement to check if the $_POST['price'] is with the option value. If the value is matched, add the attribute 'selected' to that option.

Can you help me with a sample? I can't figure that out. Thanks

//Try this

    <select name='price' id='price'>
        <option <?=((isset($_POST['price']) && $_POST['price'] == '')?'SELECTED="SELECTED"':'')?> value='' >Select....</option>
        <option <?=((isset($_POST['price']) && $_POST['price'] == '0-50,000')?'SELECTED="SELECTED"':'')?> value='0-50,000'>0-50,000</option>
        <option <?=((isset($_POST['price']) && $_POST['price'] == '50,000-100,000')?'SELECTED="SELECTED"':'')?> value='50,000-100,000'>50,000-100,000</option>
        <option <?=((isset($_POST['price']) && $_POST['price'] == '100,000-150,000')?'SELECTED="SELECTED"':'')?> value='100,000-150,000'>100,000-150,000</option>
        <option <?=((isset($_POST['price']) && $_POST['price'] == '150,000-200,000')?'SELECTED="SELECTED"':'')?> value='150,000-200,000'>150,000-200,000</option>
        <option <?=((isset($_POST['price']) && $_POST['price'] == '200,000 and above')?'SELECTED="SELECTED"':'')?> value='200,000 and above'>200,000 and above</option>
        <option <?=((isset($_POST['price']) && $_POST['price'] == 'see all')?'SELECTED="SELECTED"':'')?> value='see all'>See All</option>
    </select>
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.