【问题标题】:PHP file receives blank values from html form [AJAX]PHP 文件从 html 表单 [AJAX] 接收空白值
【发布时间】:2014-10-05 20:24:16
【问题描述】:

我的目标是使用 AJAX,但我在这方面有点新手,让用户可以搜索某些内容并获取有关该内容的数据库行。我测试了几次,尝试调试,但找不到解决方法。谢谢!

HTML 代码:

<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("jotamacho").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("jotamacho").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","output.php",true);
xmlhttp.send();
}
</script>


</head>

<body>

<form action="output.php" method="post"  onchange="showUser(this.value)">
<select name="feature">
<option value="Nome">Primeiro Nome</option>
<option value="Apelido">Apelido</option>
<option value="País">País de origem</option>
<option value="Data">Data de nascimento</option>
</select>


Search: <input type="text" name="textquery" onchange="showUser(this.value)><br>


<br>
<div id="jotamacho">Here !</div>  

</form>
</body>
</html>

还有我的 output.php 文件:

<?php

$con=mysqli_connect("127.0.0.1","root","","");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$search =mysqli_real_escape_string($con, $_POST['textquery']);
$coluna =mysqli_real_escape_string($con, $_POST['feature']);
$resposta = mysqli_query($con,"SELECT Nome,Apelido,Data_nascimento,País FROM Pessoa   WHERE Pessoa.$coluna like '$search' ");

echo " <b> Results:";

echo "<table border='5'>
<tr>
<th>Nome</th>
<th>Apelido</th>
<th>Data de Nascimento</th>
<th>País</th>
</tr>";


while($row = mysqli_fetch_array($resposta)) { 
echo "<tr>";
echo "<td>" . $row['Nome'] . "</td>";
echo "<td>" . $row['Apelido'] . "</td>";
echo "<td>" . $row['Data_nascimento'] . "</td>";
echo "<td>" . $row['País'] . "</td>";
echo "</tr>";
}

echo "</table>";

mysqli_close($con);
?>

当我测试它时:

注意:未定义索引:第 15 行 E:\XAMPP\htdocs\Bioinformatica\AJAX\output.php 中的文本查询

注意:未定义索引:第 16 行 E:\XAMPP\htdocs\Bioinformatica\AJAX\output.php 中的功能

警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在 E:\XAMPP\htdocs\Bioinformatica\AJAX\output.php 第 36 行给出

【问题讨论】:

    标签: php mysql ajax xampp


    【解决方案1】:
    <!DOCTYPE html>            
    <html>            
    <head>            
    <script>            
    function showUser() {            
    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.status==200) {            
    document.getElementById("jotamacho").innerHTML=xmlhttp.responseText;            
    }            
    }            
    var fea=document.getElementById("feature").value;            
    var txq=document.getElementById("textquery").value;            
    xmlhttp.open("POST","output.php",true);            
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");            
    xmlhttp.send("feature=" + fea + "&textquery=" + txq);            
    }            
    </script>            
    
    
    </head>            
    <body>            
    <select id="feature">            
    <option value="Nome">Primeiro Nome</option>            
    <option value="Apelido">Apelido</option>            
    <option value="País">País de origem</option>            
    <option value="Data">Data de nascimento</option>            
    </select>            
    Search: <input type="text" id="textquery" onchange="showUser()"><br>            
    <br>            
    <div id="jotamacho">Here !</div>              
    </body>            
    </html>
    

    【讨论】:

    • 仍然是空变量:/
    • 通知不见了!但是当我插入文本时不显示任何数据库行:/
    • 啊抱歉 pesquisa 是我发帖时翻译过来的,xD 在葡萄牙语中的意思是“搜索”:P。
    猜你喜欢
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多