在更改时从选择框中获取价值

时间:2014-11-02 20:41:35

标签: php html

我尝试在更改所选索引后从选择框中检索值。我一直得到一个未定义的索引变量。

表单重新加载页面,以便我可以在页面的其他位置更新表。这些选项是从SQL查询的结果中填充的。

选择框代码。

<form action="" method="post">
    <label>Select School</label>
    <select class="form-control" name="schoolSelect" onchange="this.form.submit()">
        <?php
        foreach ($faculty as $key) { ?>

        <option value="<?php echo $key['1']; ?>"><?php echo $key['1']; ?></option>

        <?php } ?>
    </select>
</form>

PHP用于检索值

if (isset($_POST['schoolSelect'])){
    $selectedSchool = $_POST['schoolSelect'];

    $result = executeUserSelect($sqlUserBySchool, $db, $_POST['schoolSelect']);
}

修改

var dump =

array (size=1)
  'schoolSelect' => string 'Plymouth Business School' (length=24)

选择方框文字=普利茅斯商学院

提前致谢

1 个答案:

答案 0 :(得分:0)

<body>
<?php
if (isset($_POST['schoolSelect'])){
    $selectedSchool = $_POST['schoolSelect'];

    echo $selectedSchool;
}
else {
?>

<form action="" method="post">
    <label>Select School</label>
    <select class="form-control" name="schoolSelect" onchange="this.form.submit()">
        <?php
        foreach ($faculty as $key) { ?>

        <option value="<?php echo $key['1']; ?>"><?php echo $key['1']; ?></option>

        <?php } ?>
    </select>
</form>
<?php } ?>
</body>