我有一个显示用户名的下拉列表。如何使用Ajax显示其信息?

时间:2016-10-24 20:45:52

标签: php jquery mysql ajax

我的下拉列表显示了我从数据库中获取的用户名。我想实现displayInfo功能,以便当有人选择用户时,它会自动显示下面的信息。

当有人选择他们的名字时,如何显示用户的信息?

这是我的下拉代码:

<?php 
//connect                                                 
$conn = mysqli_connect("localhost","user","123abc");
mysqli_select_db($conn, "users");

//query
$sql= mysqli_query($conn, "SELECT person_id,first_name FROM users");

echo "<select name='dropdown' onchange='displayInfo' id='dropdown'>";
while ($row = mysqli_fetch_array($sql))     
{
    //display friends' first names on dropdown
    if($row['person_id'] == $row['first_name']) {
        echo "<option value='" . $row['person_id'] . "' selected>" . $row['list_name'] . "</option>";
    } else {
        echo "<option value='" . $row['person_id'] . "'>" . $row['first_name'] . "</option>";
    }

}
echo "</select>";

1 个答案:

答案 0 :(得分:0)

在选项选择上调用Javascript函数的最佳方法

<?php 
//connect                                                 
$conn = mysqli_connect("localhost","user","123abc");
mysqli_select_db($conn, "users");

//query
$sql= mysqli_query($conn, "SELECT person_id,first_name FROM users");

echo "<select name='dropdown' onchange='displayInfo' id='dropdown'>";
while ($row = mysqli_fetch_array($sql))     
{
    //display friends' first names on dropdown
    if($row['person_id'] == $row['first_name']) {
        **echo "<option onchange='myFunction(this);' value='" . $row['person_id'] . "' selected>" . $row['list_name'] . "</option>";**
    } else {
        echo "<option onchange='myFunction(this);' value='" . $row['person_id'] . "'>" . $row['first_name'] . "</option>";
    }

}
echo "</select>";

<script>
function myFunction(control){

$(control).val() //to access value or any other functionality
}
</script>