Question

I have a select option like this

            <select name="select1">
                <option value="value1">hots</option>
                <option value="value2">popular</option>
                <option value="value3" >new</option>
                <option value="value4">old</option>
            </select>

when a user select one option in the next loading the selected option again is the first one (value1), i managed to find an official solution, i used a switch case, like this:

$panir=$_POST['select1'];
    switch ($panir) {
        case 'value1':
            ?>
            <select name="select1">
                <option value="value1" selected>hot</option>
                <option value="value2">popular</option>
                <option value="value3" >new</option>
                <option value="value4">old</option>
            </select>
            <?php
            break;
        case 'value2':
            ?>
            <select name="select1">
                <option value="value1">hot</option>
                <option value="value2" selected>popular</option>
                <option value="value3">new</option>
                <option value="value4">old</option>
            </select>
            <?php
            break;}

it works great, but has a big problem, i have another select option with lots of options and i can't add case as number as them, isn't there any solution that options would be selected by a condition? thanks a lot.

Was it helpful?

Solution

You should have only 1 select. Before the ">" of each option you can add

<?php echo ( $_POST['select1'] == 'value' ? 'selected' : '' ); ?>

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top