简体   繁体   English

在用户选择php的选项选择下拉菜单中设置默认值

[英]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. 首先,假设用户已选择一个ID来编辑特定数据。

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. 但是我的问题是,它没有突出显示用户需要编辑的特定数据,并且如果用户将其保存,该值将为0或null。

Error1 Error2 ERROR1 误差2

I want to highlight the specific data where the user need to edit like this one.. 我想突出显示用户需要像这样编辑的特定数据。

Hihglighted Hihglighted

I have 2 queries: Here's are all my query to get the subject , semester/term and schoolyear: 我有2个查询:这是获取主题,学期/学期和学年的所有查询:

$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.. 这是我的查询,以获取主题代码和主题描述,用户可以在其中选择要编辑的ID。

$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>

There is no Term_ID in select clause of your second query. 第二个查询的select子句中没有Term_ID Also, you are missing value attribute in your disabled option. 此外,您在禁用的选项中缺少value属性。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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