Html 选择页面重置时字段重置

Html 选择页面重置时字段重置,html,forms,select,reset,Html,Forms,Select,Reset,我编写了一个代码,用户可以在选择字段中选择一个选项。它有onchange=location=this.options[this.selectedIndex].value; 选择标记和每个选项标记都有自己的值 <?php parse_str($_SERVER['QUERY_STRING']); require ('../config.php');

我编写了一个代码,用户可以在选择字段中选择一个选项。它有onchange=location=this.options[this.selectedIndex].value; 选择标记和每个选项标记都有自己的值

                    <?php

                    parse_str($_SERVER['QUERY_STRING']);

                    require ('../config.php');
                     /* connect and select db codes*/

                    $dbresult=mysqli_query($connection, "SELECT * FROM services");

                    while($row = mysqli_fetch_array($dbresult)){
                            $order_name = $row['name'];
                            $order_id = $row['id'];
                            echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\">$order_name</option>";
                    }
                    ?>

                            </select><br/>


  /* ...continue of my codes...*/
所以我的问题是:

                    <?php

                    parse_str($_SERVER['QUERY_STRING']);

                    require ('../config.php');
                     /* connect and select db codes*/

                    $dbresult=mysqli_query($connection, "SELECT * FROM services");

                    while($row = mysqli_fetch_array($dbresult)){
                            $order_name = $row['name'];
                            $order_id = $row['id'];
                            echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\">$order_name</option>";
                    }
                    ?>

                            </select><br/>


  /* ...continue of my codes...*/
当我从这个选择字段中选择一个选项时,它会将查询字符串发送到同一个页面,我希望我选择的选项保持选中状态,但当页面重置时,选择字段被重置。 事实并非如此,但我认为我在代码中做了一些改变,但我找不到,这就产生了问题

                    <?php

                    parse_str($_SERVER['QUERY_STRING']);

                    require ('../config.php');
                     /* connect and select db codes*/

                    $dbresult=mysqli_query($connection, "SELECT * FROM services");

                    while($row = mysqli_fetch_array($dbresult)){
                            $order_name = $row['name'];
                            $order_id = $row['id'];
                            echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\">$order_name</option>";
                    }
                    ?>

                            </select><br/>


  /* ...continue of my codes...*/
这是我的密码:

                    <?php

                    parse_str($_SERVER['QUERY_STRING']);

                    require ('../config.php');
                     /* connect and select db codes*/

                    $dbresult=mysqli_query($connection, "SELECT * FROM services");

                    while($row = mysqli_fetch_array($dbresult)){
                            $order_name = $row['name'];
                            $order_id = $row['id'];
                            echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\">$order_name</option>";
                    }
                    ?>

                            </select><br/>


  /* ...continue of my codes...*/

必须指定在属性中选择该选项。 因此,对于要选择的选项,需要添加selected=selected属性

                    <?php

                    parse_str($_SERVER['QUERY_STRING']);

                    require ('../config.php');
                     /* connect and select db codes*/

                    $dbresult=mysqli_query($connection, "SELECT * FROM services");

                    while($row = mysqli_fetch_array($dbresult)){
                            $order_name = $row['name'];
                            $order_id = $row['id'];
                            echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\">$order_name</option>";
                    }
                    ?>

                            </select><br/>


  /* ...continue of my codes...*/
在PHP中,这可以在echo语句之前使用类似的方法完成,如果订单id与查询字符串中指定的订单id相同,它将选择此选项

                    <?php

                    parse_str($_SERVER['QUERY_STRING']);

                    require ('../config.php');
                     /* connect and select db codes*/

                    $dbresult=mysqli_query($connection, "SELECT * FROM services");

                    while($row = mysqli_fetch_array($dbresult)){
                            $order_name = $row['name'];
                            $order_id = $row['id'];
                            echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\">$order_name</option>";
                    }
                    ?>

                            </select><br/>


  /* ...continue of my codes...*/
  if($service == $order_id){
     $selected = "selected";
  }
  else{
     $selected = "";
  }
在你的回音声明中

                    <?php

                    parse_str($_SERVER['QUERY_STRING']);

                    require ('../config.php');
                     /* connect and select db codes*/

                    $dbresult=mysqli_query($connection, "SELECT * FROM services");

                    while($row = mysqli_fetch_array($dbresult)){
                            $order_name = $row['name'];
                            $order_id = $row['id'];
                            echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\">$order_name</option>";
                    }
                    ?>

                            </select><br/>


  /* ...continue of my codes...*/
   echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\" ".$selected.">$order_name</option>";