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?

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.