繁体   English   中英

使用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>";
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM