如何使用php和mysql中的select选项过滤talbe

时间:2017-09-03 03:52:47

标签: javascript php mysql

我想使用select选项来更新表。这些是代码,当我尝试它时,没有数据显示到table..im使用js但仍然没有任何事情..只显示字段但没有数据。帮帮我吧。



<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 (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.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">150-2012-00007</option>
  <option value="2">120-2013-001</option>
  <option value="3">130-2012-0022</option>
  <option value="4">130-2012-0554</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>
&#13;
&#13;
&#13;

这是getuser.php的代码

&#13;
&#13;
<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']);

 $db_host = 'localhost'; // Server Name
 $db_user = 'root'; // Username
 $db_pass = ''; // Password
 $db_name = 'ofes'; // Database Name

 $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
    if (!$conn) 
        {
            die ('Failed to connect to MySQL: ' . mysqli_connect_error());	
        }else{
            echo "";
        }
    
$sql="SELECT * FROM tbl_student WHERE Id_number = '".$q."'";
$result = mysqli_query($conn,$sql);

echo "<table>
<tr>
<th>Name</th>
<th>Id number</th>
<th>Passwordr</th>
<th>Course</th>
<th>School Year</th>
<th>Semester</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['Id_number'] . "</td>";
    echo "<td>" . $row['password'] . "</td>";
    echo "<td>" . $row['course'] . "</td>";
    echo "<td>" . $row['school_year'] . "</td>";
    echo "<td>" . $row['semester'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
&#13;
&#13;
&#13;

基本上当我从select选项中选择时,表格会自动更新。

0 个答案:

没有答案