我想在php中创建一个搜索功能,它获取输入的值并在数据库中搜索它。 我有这段代码:
<center><form method="post" id="search" action="search.php?go">
<p>
<input type="text" name="name" id="name" />
<input type="submit" id="search_btn" name="submit" value="Cerca utente" />
</p>
</form></center>
在php文件中我创建了这个:
public function searchUser($nomeUtente)
{
$sql = "SELECT uUsername FROM users WHERE='$nomeUtente' ";
if( !$this->stmt = $this->mysqli->prepare($sql) )
throw new Exception("MySQL Prepare statement failed: ".$this->mysqli->error);
$this->stmt->execute();
$this->stmt->store_result();
$this->stmt->bind_result($nome);
if( $this->stmt->num_rows == 0)
return "Nessun nome corrispondente.";
$nomi = array();
$i = 0;
while( $this->stmt->fetch() ){
$nomi[$i]["nome"] = $nome;
}
return $nomi;
}
如何在当前页面的表格中查看查询结果?我不想在另一个页面中查看结果,但是在搜索表单所在的页面中。
答案 0 :(得分:0)
***HTML***
<center><form method="post" id="search" action="search.php?go">
<p>
<input type="text" name="name" id="name" />
<input type="submit" id="search_btn" name="submit" value="Cerca utente" />
</p>
</form></center>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$('#search_btn').click(function(){
var Name=$('#name').val();
$.Post('Search.php',{Name:Name},function(data)
{
alert(data);
});
});
});
</script>
PHP(必须保存为search.php)
<?php
if($_isset($_POST["Name"]))
{
$Name=$_POST["Name"];
searchUser($Name);
public function searchUser($nomeUtente)
{
$sql = "SELECT uUsername FROM users WHERE='$nomeUtente' ";
if( !$this->stmt = $this->mysqli->prepare($sql) )
throw new Exception("MySQL Prepare statement failed: ".$this->mysqli->error);
$this->stmt->execute();
$this->stmt->store_result();
$this->stmt->bind_result($nome);
if( $this->stmt->num_rows == 0)
return "Nessun nome corrispondente.";
$nomi = array();
$i = 0;
while( $this->stmt->fetch() ){
$nomi[$i]["nome"] = $nome;
}
return $nomi;
}
}
?>