php-auto加载选定的下拉值

时间:2015-03-09 08:35:57

标签: php

我有一个下拉菜单..我想在页面打开时自动加载所选值。但我不知道该怎么做。任何人都可以帮忙吗?

<select name="num" onchange="this.form.submit()">

        <option value="25" selected>25</option>
        <option value="50">50</option>
        <option value="75">75</option>
        <option value="100">100</option>
    </select>

例如,当我转到该页面时,页面自动显示25条记录给我。

1 个答案:

答案 0 :(得分:0)

您的网页必须以.php扩展名命名,您必须有权访问该结果:

    <?php
    //let's assume you take selected value from $_POST
    if(isset($_POST['num']){
        $selected_option = $_POST['num'];
    }else{
        $selected_option = '';
    }

    $options = array(25,50,75,100);
    ?>
    <form id="myForm" method="post" action = "">
    <select name="num" onchange="document.getElementById('myForm').submit()">
    <?php
    foreach($options as $v){
        if($v == $selected_option){
            $selected = 'selected = "selected"';
        }else{
            $selected = '';
        }
        echo "<option value='$v' $selected>$v</option>";
    }
    ?>    
    </select>
    </form>