Jerico Borja
Jerico Borja

Reputation: 5

set default value in option select dropdown which the user selected php

First let's assume that the user has selected an ID to edit the specific data.

Here's where the user select to edit

Output will be here

But my problem is that it's not highlighting the specific data where the user need to edit and if the user save it the value will be 0 or null.

Error1 Error2

I want to highlight the specific data where the user need to edit like this one..

Hihglighted

I have 2 queries: Here's are all my query to get the subject , semester/term and schoolyear:

$sql1 = "select * from subject";
$subject = $conn -> query($sql1);

$sql2 = "select * from sy";
    $sy = $conn -> query($sql2);

$sql3 ="select * from term";
$semester = $conn -> query($sql3);

and here's my query to get the subject code and the subject description where the user select the id to edit..

$sql4 = "select subject.Subject_Description ,subject.Subject_Code, subject_offer.Subject_Offer_ID, term.Term_Name
                from subject_offer
                inner join subject on subject_offer.Subject_Code = subject.Subject_Code
                inner join term on subject_offer.Term_ID = term.Term_ID
                where subject_offer.Subject_Offer_ID = '$id'";
    $result = $conn -> query($sql4)->fetch_object();

Here's the code for my dropdown or option select..

<div class="form-group row">
                                <label for="inputEmail3" class="col-sm-2 col-form-label">Subject</label>
                                <div class="col-sm-7">
                                    <select class="form-control  form-control-md col-sm-12" id="inputSmall" name="subject">
                                        <option  disabled selected ><?php echo $result->Subject_Code . '' .$result->Subject_Description ?></option>
                                            <?php while($row1 = $subject->fetch_object() ):  ?>
                                            <option  value="<?php echo $row1->Subject_Code ?>" >
                                                <?php echo $row1->Subject_Code.' &nbsp;&nbsp; '.$row1->Subject_Description; ?>
                                            </option>
                                            <?php endwhile; ?>
                                    </select>
                                </div>
                            </div>

Upvotes: 0

Views: 49

Answers (1)

Furgas
Furgas

Reputation: 2844

There is no Term_ID in select clause of your second query. Also, you are missing value attribute in your disabled option. Should be:

<option  disabled selected value="<?php echo $result->Subject_Code ?>">
    <?php echo $result->Subject_Code . '&nbsp;&nbsp;' .$result->Subject_Description ?>
</option>

I usually do it differently, that is, mark the selected option when its value is the current one:

<select class="form-control  form-control-md col-sm-12" id="inputSmall" name="subject">
    <?php while($row1 = $subject->fetch_object() ):  ?>
        <option value="<?php echo $row1->Subject_Code ?>" <?= $row1->Subject_Code === $result->Subject_Code ? 'selected' : '' ?>>
            <?php echo $row1->Subject_Code.' &nbsp;&nbsp; '.$row1->Subject_Description; ?>
        </option>
    <?php endwhile; ?>
</select>

Upvotes: 1

Related Questions