使用 GET 方法提交表单后保留下拉值


Retaining dropdown values after submitting form using GET method

我正在使用下拉列表来选择值,单击提交按钮后,我的值会更改。请帮助我保留所选值。使用 POST 方法我已经得到了解决方案,但我想与 GET 方法一起使用。可能吗?

1.) 第一个选择 STMT:

<form action="" method="GET">
   <select name="sort"  >
     <option value="inc_patientName">Patient Name</option>
     <option value="inc_date">Date</option>
     <option value="inc_status">Status</option>
     <option value="inc_patientAge">Age</option>
   </select>
   <input type="submit" name="GETREPORT" value="Get Report"/>
   if ($_GET)
   {echo"hi";}
</form>

2.) 第二选

<?php
    //Selecting ward from table ward master
    $sql = "SELECT ward_name,ward_id FROM ward_master";
    $result = mysql_query($sql);
    echo "<select name='ward'>";
    while ($row = mysql_fetch_array($result)) 
    {
    echo "<option value='" . $row['ward_id'] . "'>" . $row['ward_name'] . "</option>";
    }
    echo "</select>";
    ?>

3.) 使用 JavaScript 进行第 3 次选择:

<select name="daydropdown" id="daydropdown" ></select> 
    <select name="monthdropdown" id="monthdropdown"></select>
    <select name="yeardropdown" id="yeardropdown"></select>
    <script type="text/javascript">
    populatedropdown("daydropdown", "monthdropdown", "yeardropdown")

Javascript代码:

<script type="text/javascript">
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
function populatedropdown(dayfield, monthfield, yearfield)
{
   var today=new Date()
   var dayfield=document.getElementById(dayfield)
   var monthfield=document.getElementById(monthfield)
   var yearfield=document.getElementById(yearfield)
   for (var i=1; i<=31; i++)
   dayfield.options[i]=new Option(i, i)
   dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
   for (var m=0; m<12; m++)
   monthfield.options[m]=new Option(monthtext[m], monthtext[m])
   monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
   var thisyear=1999
   for (var y=0; y<45; y++){
   yearfield.options[y]=new Option(thisyear, thisyear)
   thisyear+=1
   }
   yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}    
</script>

试试

if(isset($_GET['sort'])) {
  echo $_GET['sort'];
}

并获取所选索引

<option value="inc_patientName" <?php if (isset($_GET['sort']) && $_GET['sort'] == "inc_patientName") echo 'selected="seleceted"'; ?>>Patient Name</option>

依此类推,所有选项值都匹配

对于第二个下拉菜单:-

while ($row = mysql_fetch_array($result)) {?>
    <option value="<?php echo $row['ward_id'];?>" <?php if (isset($_GET['sort']) && $_GET['sort'] == $row['ward_id']) echo 'selected="seleceted"'; ?>><?php echo $row['ward_name'];?></option>
    <?php }

这样使用它...它我的工作。

<select name="ward">
<?php 
    $sql = "SELECT ward_name,ward_id FROM ward_master";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)) { 
        if ($_GET['ward']==$row["ward_id"]) {
           echo '<option selected="selected" value='.$row["ward_id"].'>'.$row["ward_name"].'</option>';
        } else {
           echo '<option value='.$row["ward_id"].'>'.$row["ward_name"].'</option>';
        }
    }
?>
</select>

你可以做这样的事情:

<select name="sort"  >
        <option value="inc_patientName"<?php if ($_GET['sort'] == 'inc_patientName') echo ' selected="seleceted"'; ?>>Patient Name</option>
        <option value="inc_date"<?php if ($_GET['sort'] == 'inc_date') echo ' selected="seleceted"'; ?>>Date</option>
        <option value="inc_status" <?php if ($_GET['sort'] == 'inc_status') echo ' selected="seleceted"'; ?>>Status</option>
        <option value="inc_patientAge"<?php if ($_GET['sort'] == 'inc_patientAge') echo ' selected="seleceted"'; ?>>Age</option>
        </select>

试试这个:在脚本中使用此函数:

function setday(id,elementname)
{
document.getElementById(elementname).value=id;
}

在你的 php 表单中使用它,对年份和月份下拉列表重复相同的代码:

if (isset($_GET['daydropdown']))
            {
           echo "<script type='text/javascript'>setday('".$_GET['daydropdown']."','daydropdown')</script>";
            }