AJAX-如何在选择时显示不同的列


AJAX - How To Display Different Columns Upon Selection

我正在使用AJAX从MySQL数据库中获取数据。它的工作方式:用户从下拉菜单中选择一个项目,然后显示数据。然而,我的问题是:有没有办法在同一下拉菜单中显示不同的列?换言之,下拉菜单中的第一个选择显示名字和姓氏,下拉菜单的第二个选择显示姓名和后缀。似乎不可能在下拉菜单的一个选择中包含一列,但在另一个选择中将不包含。但我至少想问一个问题,看看我是否遗漏了什么。我的两个脚本:

index.php:

<html>
<head>
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
  <option value="">Select a person:</option>
  <option value="1">Peter Griffin</option>
  <option value="2">Lois Griffin</option>
  <option value="3">Joseph Swanson</option>
  <option value="4">Glenn Quagmire</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
</body>
</html>

getuser.php:

<!DOCTYPE html>
<html>
<head>
<style>
table {
    width: 100%;
    border-collapse: collapse;
}
table, td, th {
    border: 1px solid black;
    padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Age'] . "</td>";
    echo "<td>" . $row['Hometown'] . "</td>";
    echo "<td>" . $row['Job'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

您可以使用ifswitch语句来更改返回的HTML。

echo "<table>";
switch ($q) {
case "1":
    echo "<tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";
    break;
case "2":
    echo "<tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Suffix</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";
    break;
default:
    echo "<tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";
    break;
}
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    echo "<td>" . $row['LastName'] . "</td>";
    if ($q == 2) {
        echo "<td>" . $row['Suffix'] . "</td>";
    }
    if ($q < 3) {
        echo "<td>" . $row['Age'] . "</td>";
    }
    echo "<td>" . $row['Hometown'] . "</td>";
    echo "<td>" . $row['Job'] . "</td>";
    echo "</tr>";
}
echo "</table>";