Home > Blockchain >  PHP Keep drop down value after submitting from database
PHP Keep drop down value after submitting from database

Time:10-27

I want to keep my drop-down value after on change submit.

HTML code

<select id="Doctor" name="Doctor" onchange="this.form.submit();">
                    <?php
                    $Store->displayDoctors()
                    ?>
</select>

PHP Code

public function displayDoctors(){
        $sql = "SELECT * FROM doctor";
        $result = @mysqli_query($this->conn, $sql);
        while($data = mysqli_fetch_array($result))
        {
            echo "<option value='". $data['docID'] ."'>" .$data['FirstName']." ".$data['LastName'] ."</option>";  
        }     
    }

CodePudding user response:

I think you can use like: HTML:

<select id="Doctor" name="Doctor" onchange="this.form.submit();">
                    <?php
                    $Store->displayDoctors($_REQUEST['Doctor'])
                    ?>
</select>

PHP:

public function displayDoctors($value=false){
        $sql = "SELECT * FROM doctor";
        $result = @mysqli_query($this->conn, $sql);
        while($data = mysqli_fetch_array($result))
        {
            $selected="";
            if($data['docID']==$value)
            {
              $selected="selected";
            }
            echo "<option value='". $data['docID'] ."' $selected>" .$data['FirstName']." ".$data['LastName'] ."</option>";  
        }     
    }
  • Related