提问人:Jonny1998 提问时间:6/27/2018 最后编辑:Jonny1998 更新时间:6/27/2018 访问量:202
使用 PHP 和 AJAX 选择器表单查询 MySQL
Query MySQL using PHP and AJAX selector form
问:
我正在编写一些PHP来从我在WAMP服务器上设置的MySQL数据库进行查询。我也在学习PHP和html javascript,所以这两种语言的语法对我来说还是有点陌生。
我正在通过我的服务器运行两个文件,front.php 和 back.php。front.php包含一个选择器表单,用户可以在其中选择要应用于MySQL的PHP查询的过滤器。back.php接收带有 $_REQUEST 的选择,并将其用于对 MySQL 的 SELECT 查询。我已经在下面的 front.php 中发布了与选择器表单相关的代码。
编译时收到“Undefined index: family”错误。 此外,我还包含了 front.php 和 back.php 文件以供参考。
我非常感谢任何帮助!
<form method="POST">
<select name="family" onchange="showUser (this.value)">
<option value="empty">Select a Family:</option>
<option value="capacitor">capacitor</option>
<option value="resistor">resistor</option>
<option value="ferrite bead">ferrite bead</option>
</select>
</form>
这是 _REQUEST 美元的看涨期权,它收到了上述选择.php
$sql="SELECT * FROM testv2 WHERE family='".$_REQUEST['family']."'";
$result = mysqli_query($con,$sql);
前面。PHP的
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").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 (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","back.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="family" onchange="showUser(this.value)">
<option value="empty">Select a Family:</option>
<option value="capacitor">capacitor</option>
<option value="resistor">resistor</option>
<option value="ferrite bead">ferrite bead</option>
</select>
</form>
<br>
<div id="txtHint"><b>Filter Info to be Displayed Here.</b></div>
</body>
</html>
返回。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
$con = mysqli_connect('localhost','root','kelly188','mysql');
mysqli_select_db($con,"testv2");
$sql="SELECT * FROM testv2 WHERE family='".$_REQUEST['family']."'";
$result = mysqli_query($con,$sql);
return var_dump($sql);
echo "<table>
<tr>
<th>ID</th>
<th>Family</th>
<th>Capacitance</th>
<th>Voltage</th>
<th>Price</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['family'] . "</td>";
echo "<td>" . $row['capacitance'] . "</td>";
echo "<td>" . $row['voltage'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
第一张图显示了包含var_dump回车符的代码 第二个图像是当我运行代码时在服务器窗口中编译的内容
答: 暂无答案
评论
var_dump($_REQUEST)