搜索功能php和mysql查看当前页面中的结果
我想在php中创建一个搜索功能,它获取输入值并在db中搜索它。 我有以下代码:搜索功能php和mysql查看当前页面中的结果,php,html,mysql,Php,Html,Mysql,我想在php中创建一个搜索功能,它获取输入值并在db中搜索它。 我有以下代码: <center><form method="post" id="search" action="search.php?go"> <p> <input type="text" name="name" id="name" /> <input type="submit" id="search_btn"
<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>
如何在当前页面的表中查看查询结果?我不想在其他页面中查看结果,而是在搜索表单所在的页面中查看结果。***HTML***
***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>
$(文档).ready(函数(){
$('search_btn')。单击(函数(){
var Name=$('#Name').val();
$.Post('Search.php',{Name:Name},函数(数据)
{
警报(数据);
});
});
});
PHP(必须另存为search.PHP)
为此,必须使用Ajax jquery调用php函数。您可以在当前页面div中替换结果div,而无需刷新当前页面。
***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
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;
}
}
?>