如何使用ajax,PHP和mysql执行实时搜索 [英] How to perform live search using ajax, PHP, and mysql

查看:73
本文介绍了如何使用ajax,PHP和mysql执行实时搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ajax,PHP和MYSQL进行实时搜索。我的代码正在运作。但是,主要问题是,一旦在数据库中找到用户,我就无法将用户重定向到他们的个人资料页面。我检查了console.log,它有一个指向他们的个人资料页面的链接。这是结果 https://imgur.com/a/I8tXMvH



HTML

I am trying to perform a live search using Ajax,PHP, and MYSQL. The code I have is working. However, the main problem is I can't redirect the user to their profile page once I found the user within the database. I checked the console.log and it has a link to the their profile page. This is the results https://imgur.com/a/I8tXMvH

HTML

<div class="inner-addon left-addon">
          
          <input class="form-control" type ="text" id ="search"  placeholder="Search for employees..." onkeyup="search(this.value)">
          <div id ="results"></div>
      </div





Ajax代码



Ajax Code

      function search(value) {
      if(value.length == 0) 
      {
        $("#results").html("");
      } else {

        $.post("employees.php", {search:value}, function(data){
          $("#results").html(data);
        });
 }
}





PHP代码



PHP Code

    $search = $_POST['search'];
  $query =" SELECT * FROM employees WHERE firstName LIKE '%$search%' OR lastName LIKE '%$search%'";
   $query = mysqli_query($connect,$query);
  while($row= mysqli_fetch_array($query)){
    echo "<div>";
    echo $row['firstName']."  ".$row['lastName'];
    echo "<a href='profile.php?userId=" . $row["id"] . "'>"; //this is not working
    echo "</div>";

}





用户个人头像



User Profile code

<?php
 $userId = ($_GET['userId']);
    
$query = "SELECT firstName, lastName, middleName, gender, ssn, dob, organization, department, iden, position, salary,health, dental,vision, addressOne, addressTwo, apt, city, _state, zipcode, phone, email FROM employees WHERE id = {$userId}";
$results = mysqli_query($connect, $query);

   while($row = mysqli_fetch_array($results)) {
       $firstName = $row['firstName'];
       $lasttName = $row['lastName'];
       $middletName = $row['middleName'];
       $gender = $row['gender'];     
?>





我有什么尝试过:



我已经尝试了所有的东西,但我无法理解。



What I have tried:

I have tried everything and I just can't figure it out.

推荐答案

#results)。html( );
} else {
("#results").html(""); } else {


.post( employees.php,{search:value}, function (data){
.post("employees.php", {search:value}, function(data){


#results)。html(data);
} );
}
}
("#results").html(data); }); } }





PHP代码



PHP Code

    


这篇关于如何使用ajax,PHP和mysql执行实时搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆