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

时间:2015-01-22 10:02:48

标签: php html

我使用下拉列表选择值,点击提交按钮后,我的值会发生变化。请帮我保留所选的值。使用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>

4 个答案:

答案 0 :(得分:2)

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 }

答案 1 :(得分:1)

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

<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>

答案 2 :(得分:0)

您可以这样做:

<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>

答案 3 :(得分:0)

试试这个: 在你的脚本中使用这个功能:

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

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

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