开发者

Remember Dropdown Selection

I've got a few select lists on my form which look a bit like this:

<option value="400000" <?php if($_GET['MaxPrice'] == "400000") { echo("selected"); } ?>>&pound;400,000</option>

As you can see (above) I've got a bit of PHP in there telling the form to remember it's selection upon submit.

Is there a short bit of PHP that would remember every selection without the rather heavy method I'm using above?

UPDATE:

<select name="MaxPrice" id="MaxPrice">
    <option value="9999999" selected>Price (Max)</option>
    <option value="100000" <?php if($_GET['MaxPrice'] == "100000") { echo("selected"); } ?>>&pound;100,000</option>
    <option value="200000" <?php if($_GET['MaxPrice'] == "200000") { echo("selected"); } ?>>&pound;200,000</option>
    开发者_StackOverflow<option value="300000" <?php if($_GET['MaxPrice'] == "300000") { echo("selected"); } ?>>&pound;300,000</option>
    <option value="400000" <?php if($_GET['MaxPrice'] == "400000") { echo("selected"); } ?>>&pound;400,000</option>
</select>

UPDATE 2: Is there a way to implement this technique into some JavaScript?

if (BuyRent=='buy')
document.SearchForm.MaxPrice.options[9999999]=new Option("Price (Max)","150000000");
document.SearchForm.MaxPrice.options[100000]=new Option("\u00A3100,000","100000");
document.SearchForm.MaxPrice.options[200000]=new Option("\u00A3200,000","200000");
document.SearchForm.MaxPrice.options[300000]=new Option("\u00A3300,000","300000");
document.SearchForm.MaxPrice.options[400000]=new Option("\u00A3400,000","400000");    


Usually, you use some kind of loop. Here's an example :

$my_values = array(100000, 200000, 300000, 400000);

foreach($my_values as $value) {
    echo "<option value=\"{$value}\"";
    echo ($_GET['MaxPrice'] == $value) ? 'selected="selected"';
    echo ">" . number_format($value, 0, '.', ',') . "</option>";
}

Which would print 4 tags such as <option value="100000" selected="selected">100,000</option>.

This code also uses the ternary operator, but it's obviously not mandatory, you can write the whole if/else statement if wanted.

Edit:

<select name="MaxPrice" id="MaxPrice">
    <option value="9999999">Price (Max)</option>
    <?php
    $my_values = array(100000, 200000, 300000, 400000);

    foreach($my_values as $value) {
        echo "<option value=\"{$value}\"";
        echo ($_GET['MaxPrice'] == $value) ? 'selected="selected"' : "";
        echo ">&pound; " . number_format($value, 0, '.', ',') . "</option>";
    }
    ?>
</select>


good question, it made me wonder if there exists a js plugin to remember form field out there. And the answer seems to be "Yes!"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜