简体   繁体   中英

Retain the option in the dropdown list in php

I have a dropdown with the following options:

i.)All

ii.)Public User Where The option public user is selected from the database

<select name="desc" id="selectbox">
    <?php 
    echo '<option  value="all" >All</option>';

    $a=$obj->getPublicUser();
     echo '<option  value="PublicUser" >'.$a.'</option>';

    ?>
    </select>

$a has getPublicUser() :

 public function getPublicUser() 
     {


        $publicuser=self::PUBLIC_USER;
       $result= mysql_query(" select category_desc from user_category where category_id=4");
       if($result)
       {
          while ($row = mysql_fetch_assoc($result)) 
          {
              $user=$row['category_desc'];
          }
       }
       return $user;
 }

I have used My Submit button is as follows:

<input type="submit" name="GETREPORT" value="Get Report" />

My problem is On selecting option ALL and clicking on submit button the all option is retained. But on selecting Public user and then clicking on submit button the option resets to "all". How can I make the public user retain even after the submit ? Plz help

你可以尝试一下

if (isset($_GET['desc']) && $_GET['desc'] == "PublicUser") selected="seleceted"

Please Try this, Hope it help you:-

<html>
<form name = "client_question">
<select name="desc" id="selectbox">
    <?php 
    echo '<option  value="All" >All</option>';
    echo '<option  value="PublicUser">PublicUser</option>';
    echo '<option  value="NormalUser">NormalUser</option>';
    ?>
</select>
<input type="submit" name="GETREPORT" value="Get Report" />
</form>
<script>
<?php if(isset($_GET['desc']) && $_GET['desc']) {?>
    document.getElementById("selectbox").value = "<?php echo $_GET['desc'] ?>";
<?php } ?>
</script>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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