jermina
jermina

Reputation: 101

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

Upvotes: 0

Views: 62

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72289

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>

Upvotes: 0

user092
user092

Reputation: 437

you can try with this

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

Upvotes: 1

Related Questions