点击后,PHP下拉菜单

时间:2018-01-25 17:33:53

标签: php

我在php中创建了一个下拉菜单,但是当点击一个值时,我不知道如何收集这些信息。

<html>
<body>
<?php
$mydb = new mysqli('localhost','root','','TestTimeTableSolution');
$rows = $mydb->query("SELECT DISTINCT TeacherID FROM Teacher;")->fetch_all(MYSQLI_ASSOC);
$teachers = array();

foreach ($rows as $row) {
    array_push($teachers, $row["TeacherID"]);
}

$dropdownMenu = "<select name='TeacherID' form='Teacher'><option value='Null' selected>All</option>";
foreach ($teachers as $topic) {
    $dropdownMenu .= "<option value='" . $topic . "'>" . $topic . "</option>";
}
$dropdownMenu .= "</select>";

echo $dropdownMenu;
?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

根据您的上一条评论,“我希望它是动态的,因此只要用户点击某些相关信息会弹出”,听起来您可能会想要使用Ajax / JavaScript(我将演示一个简单的jQuery示例,为了清晰起见而注明):

<?php
$mydb = new mysqli('localhost','root','','TestTimeTableSolution');
?>
<html>
<!-- Add the jQuery library -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
// Act when the document is ready
$(function(){
    // listen for the select to change
    $('select[name=TeacherID]').on('change',function(){
        // Run the ajax – you can also use the shortcut $.post method found at:
        // https://api.jquery.com/jquery.post/
        $.ajax({
            // This is the page that is going to do the data lookup/display action
            url: '/lookup.php',
            // This is how it's sending the data to that page
            type: 'post',
            // This is what is being sent ($_POST['submit'] in this case)
            data: {
                // Use $(this) to isolate the current selected element and get value (.val())
                // The value is represented as $topic in your php
                'submit': $(this).val()
            },
            // If all goes well and the page (lookup.php) returns a response
            success: function(response) {
                // Place the response into the div (see html snippet)
                $('#loadspot').text(response);
            }
        });
    });
});
</script>
<body>
<?php
$rows = $mydb->query("SELECT DISTINCT TeacherID FROM Teacher;")->fetch_all(MYSQLI_ASSOC);
$teachers = array();
foreach ($rows as $row) {
    array_push($teachers, $row["TeacherID"]);
}

$dropdownMenu = "<select name='TeacherID' form='Teacher'><option value='Null' selected>All</option>";
foreach ($teachers as $topic) {
    $dropdownMenu .= "<option value='" . $topic . "'>" . $topic . "</option>";
}
$dropdownMenu .= "</select>";

echo $dropdownMenu;
?>
<!---------------------------------------------->
<!-- THIS IS WHERE THE CONTENT WILL LOAD INTO -->
<!---------------------------------------------->
<div id="loadspot"></div>
</body>
</html>

为了实现这一点,您需要域根目录中的页面lookup.php(您可以随心所欲地创建它,但您需要在javascript url中匹配):

<强> /lookup.php

<?php
# This is what will get placed into the parent page <div id="loadspot"></div>
# Do you your php here in place of this line and return whatever "relative information" you want
print_r($_POST);

您应该查看我链接到的jQuery页面,以获取该库的更多信息和方向,并确保使用浏览器的开发人员工具来监控控制台中的javascript错误。理想情况下,您希望通过文档了解所有这些,而不是仅仅复制和粘贴并继续...

相关问题