2

I am trying to display the saved value for a dynamic dropdown that is populated from a database. My code is as below :

<?PHP
$server = "xxx";
$options = array(  "UID" => "xxx",  "PWD" => "xxx",  "Database" => "xxx");
$conn = sqlsrv_connect($server, $options);
if ($conn === false) die("<pre>".print_r(sqlsrv_errors(), true));
echo " ";

$myquery="SELECT Department FROM Change_Details WHERE id='2137'";   
$fetched=sqlsrv_query($conn,$myquery) ; 
if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));}
    while($res=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC))
    {
        $Department=$res['Department'];
    }
 ?>

<div class="container"> <!-- Department -->
    <div class="form-inline clearfix">
        <label class="col-md-5">Department initiating the Request</label>
            <label name="Department"></label>
                <div class="col-md-5">
                            <?PHP
                            echo "<select name= 'Department' class='form-control selectpicker' onChange='getState(this.value)' Required>";
                            echo '<option value="$Department">'.'--Please Select Department--'.'</option>';
                            $sql = "SELECT ID,Name FROM Departments";
                            $query = sqlsrv_query($conn,$sql);
                            $query_display = sqlsrv_query($conn,$sql);
                            while($row=sqlsrv_fetch_array($query_display,SQLSRV_FETCH_ASSOC)){
                            echo "<option value='". $row['Name']."'>".$row['Name']. '</option>';
                            }
                            echo "</select>";
                        ?>
                </div>
    </div>
</div><br/>

What is working : The dropdown is being populated perfectly, and the value is also saved into the database.

What I need : Want to display the saved value from the database and the dropdown as well for the user to edit that field again. Appreciate any help :)

1 Answer 1

1

You need to get the current user selected value, then when iterating over the result set you can do something like this:

while($row=sqlsrv_fetch_array($query_display,SQLSRV_FETCH_ASSOC)){
    if ($Department == $row['Name']) {
        echo "<option selected='selected' value='". $row['Name']."'>".$row['Name']. '</option>';
        continue;
    }

    echo "<option value='". $row['Name']."'>".$row['Name']. '</option>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Assuming $userSavedValue is $Department in this case, when I tried this out, both the selected values and the option value from database are not displaying.
@SoumyaRao just edited my answer to better reflect the solution I was referring to, If this still doesn't work maybe it's because the value in $row['Name'] is not equal to $Department, otherwise something else is interfering, like one of the values being upper case or stuff like that.
Thankyou for responding :) Unfortunately, it is not working. Cross checked the upper case and all and everything seems to be in order, this time round the option values are being displayed but not the selected value.
@SoumyaRao . I think the best way to know the problem is to echo the current user selected value. then after that inside your loop echo all the options you've added so that you can check if the user selected value is found in options you've added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.