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

时间:2018-06-12 12:00:45

标签: php

首先让我们假设用户选择了一个ID来编辑特定数据。

Here's where the user select to edit

Output will be here

但我的问题是它没有突出显示用户需要编辑的特定数据,如果用户保存它,则值为0或null。

Error1 Error2

我想突出显示用户需要编辑的特定数据,如此...

Hihglighted

我有2个问题: 这是我的所有查询,以获得主题,学期/学期和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);

这是我的查询,以获取用户选择要编辑的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();

以下是我的下拉列表或选项选择的代码..

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

1 个答案:

答案 0 :(得分:1)

第二个查询的select子句中没有Term_ID。此外,您在禁用选项中缺少值属性。应该是:

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

我通常采用不同的方式,即在选定选项的值为当前选项时标记所选选项:

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