Php 未过帐以在SQL查询中使用的HTML表单数据
这应该很简单,但我迷路了 HTML页面上的简单表单-选择男性、女性或青少年 然后将该值发送到php页面以执行SQL查询,并使用变量“$trigen”查找所有男性、女性或青少年 使用AJAX在HTML页面上显示结果 如果我手动设置$trigen,它会起作用,但当我选择此代码中所列格式的选项时,它不会起作用:-- 我的HTML:-Php 未过帐以在SQL查询中使用的HTML表单数据,php,html,mysql,ajax,Php,Html,Mysql,Ajax,这应该很简单,但我迷路了 HTML页面上的简单表单-选择男性、女性或青少年 然后将该值发送到php页面以执行SQL查询,并使用变量“$trigen”查找所有男性、女性或青少年 使用AJAX在HTML页面上显示结果 如果我手动设置$trigen,它会起作用,但当我选择此代码中所列格式的选项时,它不会起作用:-- 我的HTML:- <!DOCTYPE html> <html> <div class="entry-content">
<!DOCTYPE html>
<html>
<div class="entry-content">
<form action="/getcustomer.php" method="POST">
<label for="trigen">Choose a gender:</label>
<select id="trigen" name="trigen">
<option value="Men">Men</option>
<option value="Women">Women</option>
<option value="Junior">Junior</option>
</select>
<button class="btn btn-primary text-center btn-block btn-flat" style="h2; margin: 20px; color:black; " name="trilookup" type="submit" onclick="showResults()"> Search</button>
</form>
</div>
<div id="results">Results go here if it works....</div>
<script>
function showResults(str) {
var xhttp;
if (str == "") {
document.getElementById("results").innerHTML = "";
return;
}
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("results").innerHTML = this.responseText;
}
};
xhttp.open("GET", "getcustomer.php?q="+str, true);
xhttp.send();
}
</script>
选择性别:
男人
女人
年少者
搜寻
如果有效,请点击此处查看结果。。。。
函数显示结果(str){
var-xhttp;
如果(str==“”){
document.getElementById(“结果”).innerHTML=“”;
返回;
}
xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“结果”).innerHTML=this.responseText;
}
};
open(“GET”、“getcustomer.php?q=“+str,true”);
xhttp.send();
}
然后我的php代码在“getcustomer.php”中
您的按钮正在导致表单提交。只需添加preventDefault()
作为showResults
函数的第一行,它将阻止表单自然提交。您正在通过ajax处理表单提交。另外,您不需要在上有操作或方法,我看不到大错误,您是否测试添加mysqli->error来检查是否存在sql错误。尝试打印您的变量。考虑添加一个check with is set函数以确保变量不为空警告:您完全可以使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。感谢您查看我的代码。我还是不能让它工作。到底是什么不工作?什么时候你没有得到你想要的结果?我根本没有得到任何结果。甚至没有说结果为0。我认为这是一个错误,因为我没有在showResults(str)中定义任何str。对不起,我第一次完全错过了一些内容,请参阅修订版answer@lostinspace-那对你有什么作用?
<?php
//connect to the database
$conn=mysqli_connect('localhost','wetsuder_user1','user123','wetsuder_finder');
if($conn){
}
else{
echo "Sorry there is a connection error".mysqli_connect_error();
}
$trigen=$_POST['trigen'];
//this is the search
$sql = "SELECT id, Gender, Wetsuit FROM wp_wetsuitdata WHERE Gender = '$trigen'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["Gender"]." ".$row["Wetsuit"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
function showResults() {
preventDefault(); // prevents form submission
// get the value
let str = document.getElementById('trigen').value;
// check to make sure it's there...
if (!str) return alert("Please select an option...");
var xhttp;
......
$trigen=$_GET['q'];