我有一个选择输入,其中有几个选项作为html,我想根据下拉列表中的选定值执行查询。
<select name="test">
<option value="apple">apple</option>
<option value="banana">banana</option>
<option value="mango">mango</option>
</select>
我想使用在上面的下拉列表中选择的标准值执行以下查询。
<?php $sql=mysqli_query($con,"select * from tblsession where sessionid='".$_REQUEST['test']."'"); ?>
请帮助。
答案 0 :(得分:0)
好的,在同一页面中你将不得不使用一些javascript,这样你就可以使用AJAX了。
所以这是第一页的代码:
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("para").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("para").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="test" onchange="showUser(this.value)">
<option value="Apple">Apple</option>
<option value="Bannana">Bannana</option>
<option value="Bannana">Mango</option>
</select>
</form>
<br>
<div id="para"><b>What you will excute will be listed here...</b></div>
</body>
</html>
&#13;
这是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('host','username','password','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
$sql="SELECT * FROM test WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['apple'] . "</td>";
echo "<td>" . $row['bannana'] . "</td>";
echo "<td>" . $row['mango'] . "</td>";
}
mysqli_close($con);
?>
</body>
</html>
&#13;