在下拉列表更改时重新加载页面并传递该值

时间:2015-06-26 06:24:57

标签: php html drop-down-menu

我的页面中有一个下拉列表。当我从下拉列表中选择一个选项时,页面应重新加载并返回到同一页面,并且该下拉列表的值应该通过 下面是下拉列表的PHP代码

<select style="width: 200px;" name="location" onchange="window.location='index.php?id=' + this.value;">
    <option value="All">All</option>
    <option value="Noida Sector 1">Noida Sector 1</option>
    <option value="Noida Sector 2">Noida Sector 2</option>
    <option value="Noida Sector 3">Noida Sector 3</option>
    <option value="Noida Sector 4">Noida Sector 4</option>
    <option value="Noida Sector 5">Noida Sector 5</option>
    <option value="Noida Sector 6">Noida Sector 6</option>
    <option value="Noida Sector 7">Noida Sector 7</option>
</select>

<?php $location=$_POST['id']; ?>

我正在尝试将值传递给$ location。

4 个答案:

答案 0 :(得分:5)

只需更改您的PHP代码,此代码输出选择选项并突出显示所选值(如下所示)


    <select style="width: 200px;" id="myselect" name="location" onchange="window.location='index.php?id='+this.value+'&pos='+this.selectedIndex;">
        <option value="All">All</option>
        <option value="Noida Sector 1">Noida Sector 1</option>
        <option value="Noida Sector 2">Noida Sector 2</option>
        <option value="Noida Sector 3">Noida Sector 3</option>
        <option value="Noida Sector 4">Noida Sector 4</option>
        <option value="Noida Sector 5">Noida Sector 5</option>
        <option value="Noida Sector 6">Noida Sector 6</option>
        <option value="Noida Sector 7">Noida Sector 7</option>
    </select>

    <?php
    if(isset($_GET['id']))
    {
        $location=$_GET['id'];
        echo $location;
    ?>
    <script>
        var myselect = document.getElementById("myselect");
        myselect.options.selectedIndex = <?php echo $_GET["pos"]; ?>
    </script>
    <?php
    }
    ?>

答案 1 :(得分:0)

是否可以将select放入表单(action = POST)? 你可以这样做(使用jquery):

$('select').on('change', function () {
    $(this).parent().submit();
});

答案 2 :(得分:0)

使用以下代码,

select top 1 ([Casetype] +'/'+ convert(varchar(50),CaseNo) +','+ convert(varchar(50),YEAR(GETDATE())) )as CaseNo from tbl_RecordRequisition where Casetype='FA' order by id desc

答案 3 :(得分:0)

@ Prabhjot Singh kainth,请使用此代码,

<option value="Noida Sector 3" <?php if ($_GET['id']=='Noida Sector 3'): ?>
selected=selected   
<?php endif ?>>Noida Sector 3</option>