php - 使用 PHP 和 AJAX 选择器表单查询 MySQL
问题描述
我正在编写一些 PHP 来从我在 WAMP 服务器上设置的 MySQL 数据库中查询。我还在学习 PHP 和 html javascript,所以这两种语言的语法对我来说还是有点陌生。
我正在通过我的服务器运行两个文件:front.php 和 back.php。front.php 包含一个选择器表单,用户可以在其中选择一个过滤器以应用于对 MySQL 的 php 查询。back.php 使用 $_REQUEST 接收选择,并在对 MySQL 的 SELECT 查询中使用它。我在下面的 front.php 中发布了与选择器表单相关的代码。
编译时收到“未定义索引:家庭”错误。同样,我已经包含了 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>
这是在 back.php 中接收上述选择的 $_REQUEST 调用
$sql="SELECT * FROM testv2 WHERE family='".$_REQUEST['family']."'";
$result = mysqli_query($con,$sql);
FRONT.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 返回的代码第二个图片是我运行代码时在我的服务器窗口中编译的内容
解决方案
推荐阅读
- python - 将大字符串转换为数据框
- complex-event-processing - 使用 esper cep 删除以前的事件
- python - Python:在for循环意外结果中追加列表
- ruby-on-rails - 尝试将数据库从 Heroku 拉到本地时出现带有 Rails 5.2 的 Heroku 错误(pg:pull)
- git - Git:基础变基(Java)
- electron.net - 无法获得桌面类型的窗口
- typescript - 使用 grpc-web 为 vue 配置 Envoy
- imagemagick - ImageMagick,在转换运算符链中设置相对大小
- c# - C# [Required()] 注解不应该抛出异常
- php - 更新会话数据不起作用 - CodeIgniter