2015-09-07 2 views
-1

Я хочу создать функцию поиска в php, которая получает значение ввода и ищет его в db. У меня есть этот код:функция поиска php и mysql просмотреть результат на текущей странице

<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; 

    } 

Как я могу просмотреть результат моего запроса в виде таблицы на текущей странице? Я не хочу просматривать результат на другой странице, но на странице, где находится форма поиска.

+1

Для этого нужно использовать функцию PHP вызова с помощью Ajax JQuery. Чем вы можете заменить свой div результата в текущей странице div без обновления текущей страницы. –

ответ

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; 

    } 

} 

?> 
Смежные вопросы