Ajax搜索结果以及点击结果以下载文件

问题描述 投票:0回答:1

我的索引页

    <html>
         <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <title>Some name</title>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
          <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
         </head>
         <body>
          <div class="container">
           <br />
           <h2 align="center">Search by name</h2><br />
           <div class="form-group">
            <div class="input-group">
             <span class="input-group-addon">Search</span>
             <input type="text" name="search_text" id="search_text" placeholder="Enter model / search here" class="form-control" />
            </div>
           </div>
           <br />
           <div id="result"></div>
          </div>
         </body>
        </html>

    <script>
    $(document).ready(function(){

     load_data();

     function load_data(query)
     {
      $.ajax({
   url:"fetch.php",
   method:"POST",
   data:{query:query},
   success:function(data)
   {
    $('#result').html(data);
   }
  });
 }
 $('#search_text').keyup(function(){
  var search = $(this).val();
  if(search != '')
  {
   load_data(search);
  }
  else
  {
   load_data();
  }
 });
});
</script>

和我的fetch.php页面用于从数据库表中获取数据并输出结果。我还添加了如果结果> 50的情况,它将要求输入几个字符,因为如果我不添加结果> 50的话,我的页面将花费20秒的时间显示所有数据,因为我的数据库表中有25000个条目。

<?php

$connect = mysqli_connect("localhost", "root", "PASSWORD", "DATABASE");
if(isset($_POST["query"]))
{
 $search = mysqli_real_escape_string($connect, $_POST["query"]);
 $query = "
  SELECT * FROM files 
  WHERE Name LIKE '%".$search."%'
 ";
}
else
{
 $query = "
  SELECT * FROM files ORDER BY ID
 ";
}
$result = mysqli_query($connect, $query);

if(mysqli_num_rows($result) < 500)
{
 $output1 .= '
  <div class="table-responsive">
   <table class="table table bordered">
    <div>
     <th>Name</th>
     <th>URL</th>
     <th>Extension</th>
    <th>Download</th>
    </div>
 ';

 while($row = mysqli_fetch_array($result))
 {
  $output2 .= '
   <tr>
    <td>'.$row["Name"].'</td>
    <td>'.$row["URL"].'</td>
    <td>'.$row["Extension"].'</td>
   </tr>
  ';
 }
 if(mysqli_num_rows($result) > 0)
 {
    echo "$output1";
    echo "$output2";
 }
else
{
    echo 'no results found';
}
}
else
{
 echo 'Please enter few more charecters';
}

?>

我希望为我的每个结果提供href链接,并在单击时从数据库表的./"URL“列中下载文件。

我的数据库看起来像这样:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS94dnVCNi5qcGcifQ==” alt =“在此处输入图像说明”>“ >>

并且我的当前页面是http://mss1996.ddns.net:8008/files

我尝试在outpur'array'中添加,但它破坏了我的页面。

MY INDEX PAGE

Some name <...></...>
php sql ajax search livesearch
1个回答
0
投票

如果该字段的内容确实是有效URL,则应该可以轻松添加URL字段以形成有效URL。我看到的缺少的代码是httphttps(如果它是外部链接)。如果它是页面内的相对链接,那么就可以了。然后添加</a>关闭链接。因此,您可以像这样形成表格列:

© www.soinside.com 2019 - 2024. All rights reserved.