的index.php
<html>
<head>
<script>
function sortResult(str)
{
var e = document.getElementById("firstDrop");
var str1 = e.options[e.selectedIndex].value;
var e = document.getElementById("secondDrop");
var str = e.options[e.selectedIndex].value;
if (str=="")
{
document.getElementById("result").innerHTML="";
return;
}
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("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","results.php?q="+str+"&t="+str1,true);
xmlhttp.send();
}
</script>
</head>
<body>
<!--database name-->
<select name="tablename" id="firstDrop">
<option selected='selected' value="yourdatabasename1">DB1</option>
<option value="yourdatabasename2">DB2</option>
</select>
<!--sort by what-->
<select name="sortby" id="secondDrop">
<option selected='selected' value="slno">slno</option>
<option value="name">name</option>
<option value="author">author</option>
</select>
<button id="sub" onclick="sortResult()">CLICK</button>
<br><br>
<div id="result"><b>Results will be listed here.</b></div>
</body>
</html>
results.php
<?php
$q = $_GET['q'];
$t = $_GET['t'];
$con = mysqli_connect('localhost','yourusername','password','database');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"database");
$sql="SELECT * FROM ".$t." ORDER BY ".$q;
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Author</th>
<th>ID</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['name']. "</td>";
echo "<td>" .$row['author'] ."</td>";
echo "<td>" .$row['slno']. "</td>";
echo "</tr>";
}
echo "</table>";
echo $t;
mysqli_close($con);
?>
相應地修改。
你很容易受到[SQL注入攻擊](http://bobby-tables.com) –
你的問題不是很清楚。根據我的理解,您需要在一個下拉列表中顯示所有表格,另一方面,您要顯示所有列。我是否正確? – Prashant
第一個下拉列表顯示了表格,第二個用於過濾第一個下拉列表中的選定表格 – andoni