简体   繁体   English

如何添加指向Ajax搜索结果的链接?

[英]How to add links to search results from Ajax?

I have a search bar which uses Ajax implementation to search my database and query the input data. 我有一个搜索栏,它使用Ajax实现来搜索我的数据库并查询输入数据。 view of results generated My question is how do I make the results show up as clickable link so that when clicked they go straight to the view which holds more information about them? 生成的结果视图我的问题是如何使结果显示为可单击的链接,以便在单击时可以直接进入包含有关它们的更多信息的视图? I have added the code for database query and the script used for accessing the database based on what was entered by the user in the search box. 我已经添加了数据库查询代码和用于基于用户在搜索框中输入的内容访问数据库的脚本。

<script>
  $(document).ready(function() {
    $('#search-data').unbind().keyup(function(e) {
      var value = $(this).val();
      if (value.length>3) {
        //alert(99933);
        searchData(value);
      }
      else {
        $('#search-result-container').hide();
      }
    }
                                    );
  }
                   );
  function searchData(val){
    $('#search-result-container').show();
    $('#search-result-container').html('<div><img src="preloader.gif" width="50px;" height="50px"> <span style="font-size: 20px;">Searching...</span></div>');
    $.post('controller.php',{
      'search-data': val}
           , function(data){
      if(data != "")
        $('#search-result-container').html(data);
      else    
        $('#search-result-container').html("<div class='search-result'>No Result Found...</div>");
    }
          ).fail(function(xhr, ajaxOptions, thrownError) {
      //any errors?

      alert("There was an error here!");
      //alert with HTTP error
    }
                );
  }
</script>

    <form>
<div class="manage-accounts" id="users">
      <div id="search-box-container" >
        <label > Search For Any Event: 
        </label>
        <br>
        <br>
        <input  type="text" id="search-data" name="searchData" placeholder="Search By Event Title (word length should be greater than 3) ..." autocomplete="off" />
      </div>
      <div id="search-result-container" style="border:solid 1px #BDC7D8;display:none; ">
      </div>
    </div>

</form>

database query: 数据库查询:

 <?php
include("fetch.php");
class DOA{
public function dbConnect(){
$dbhost = DB_SERVER; // set the hostname
$dbname = DB_DATABASE ; // set the database name
$dbuser = DB_USERNAME ; // set the mysql username
$dbpass = DB_PASSWORD;  // set the mysql password
try {
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
$dbConnection->exec("set names utf8");
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
public function searchData($searchVal){
try {
$dbConnection = $this->dbConnect();
$stmt = $dbConnection->prepare("SELECT * FROM events WHERE title like :searchVal");
$val = "%$searchVal%"; 
$stmt->bindParam(':searchVal', $val , PDO::PARAM_STR);   
$stmt->execute();
$Count = $stmt->rowCount(); 
//echo " Total Records Count : $Count .<br>" ;
$result ="" ;
if ($Count  > 0){
while($data=$stmt->fetch(PDO::FETCH_ASSOC)) {          
$result = $result .'<div class="search-result">'.$data['title'].'</div>';    
}
return $result ;
}
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
} 
}
?>

If all you want is making the search result clickable and browser loads the hyperlink clicked on, just echo the hyperlink from your database or JSON file depends on where they are into the html anchor element such as this: 如果您只想使搜索结果可点击,并且浏览器加载了单击的超链接,则只需从数据库或JSON文件中回显超链接,具体取决于它们在html锚元素中的位置,例如:

<a href="<?php echo $row['page_link'] ?>"><?php echo $row['page_title'] ?></a>

Note: I echoed the page link in the anchor href attribute, that should solve the problem. 注意:我在anchor href属性中回显了页面链接,这应该可以解决问题。

You can simply add some code to make a hyperlink into the HTML your PHP is generating: 您只需添加一些代码即可在您的PHP生成的HTML中建立超链接:

$result = $result .'<div class="search-result"><a href="http://events.php?id='.$data["id"].'">'.$data['title'].'</a></div>';

I have made an assumption about the name of your ID field but you can see the pattern you need to use. 我已经对您的ID字段的名称进行了假设,但是您可以看到需要使用的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM