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

时间:2016-03-30 21:51:51

标签: php mysql ajax

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用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>";