以模式显示搜索栏查询结果

时间:2018-11-28 02:53:07

标签: javascript html mysql ajax bootstrap-modal

我有一个搜索栏,供用户输入查询。点击“搜索”后,应显示查询结果的模式信息。

我在index.php中的输出仍未显示在模式中。当我单击“搜索”时,模态弹出并带有一个空的主体。 如何从index.php中获取输出以显示在模式主体中?

我的脚本有问题吗?我需要在模态主体中添加一些内容吗?

index.php

<head>
  <title>Search</title>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <link rel="stylesheet" type="text/css" href="style.css"/>
 </head>
 <body>
  <form method="POST" action="#">
   <input type="text" name="q" placeholder="Enter query"/>
   <input type="button" name="search" value="Search" data-toggle="modal" data-target="#mymodal">
   </form>
 </body>

<script>
  $.ajax({ type: "GET",
        url: 'search.php',
        success: function(data){ debugger $('#mymodal').modal('show');
        $('#mymodal:visible .modal-content .modal-body').html(e); } });
</script>

 <!-- The Modal -->
<div class="modal" id="mymodal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Modal Heading</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">

      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
      </div>

    </div>
  </div>
</div>

search.php

<?php

  include_once('db.php'); //Connect to database
  if(isset($_REQUEST['q'])){
    $q = $_REQUEST['q'];

    //get required columns
    $query = mysqli_query($conn, "SELECT * FROM `words` WHERE `englishWord` LIKE '%".$q."%' OR `yupikWord` LIKE '%".$q."%') or die(mysqli_error($conn)); //check for query error
    $count = mysqli_num_rows($query);
    if($count == 0){
      $output = '<h2>No result found</h2>';
    }else{
      while($row = mysqli_fetch_assoc($query)){
        $output .= '<h2>'.$row['yupikWord'].'</h2><br>';
        $output .= '<h2>'.$row['englishWord'].'</h2><br>';
        $output .= '<h2>'.$row['audio'].'</h2><br>';
        $audio_name = $row['audio'];
        $output .= '<td><audio src="audio/'.$audio_name.'" controls="control">'.$audio_name.'</audio></td>';
      }
    }
    echo $output;
  }else{
    "Please add search parameter";
  }

  mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:2)

使用以下search.php代码

<?php

    include_once('db.php'); //Connect to database

    if(isset($_REQUEST['q']))
    {
        $q = $_REQUEST['q'];
        $query = mysqli_query($conn, "SELECT * FROM `words` WHERE `englishWord` LIKE '%".$q."%' OR `yupikWord` LIKE '%".$q."%'") or die(mysqli_error($conn)); 
        $count = mysqli_num_rows($query);
        if($count == 0){
          $output = '<h2>No result found</h2>';
        }else{
          while($row = mysqli_fetch_assoc($query)){
            $output .= '<h2>'.$row['yupikWord'].'</h2><br>';
            $output .= '<h2>'.$row['englishWord'].'</h2><br>';
            $output .= '<h2>'.$row['audio'].'</h2><br>';
            $audio_name = $row['audio'];
            $output .= '<td><audio src="audio/'.$audio_name.'" controls="control">'.$audio_name.'</audio></td>';
          }
        }
        echo $output;
      }else{
        "Please add search parameter";
    }

    mysqli_close($conn);
?>