以模式而不是新页面加载查询搜索结果

pxiryf3j  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(687)

编辑:这里是一个截图:模态截图
编辑:我编辑了代码,当单击“搜索”按钮时,我不再被重定向到新页面,但是index.php的输出仍然没有显示在模式中。当我点击“搜索”时,模态弹出一个空的主体。如何让index.php的输出显示在modal的主体中?
我有一个搜索栏供用户输入查询。单击“搜索”后,查询结果将显示一个模式。
我的代码没有在模式窗口中显示任何内容。当我单击“搜索”按钮时,模式窗口打开,但紧接着一个新页面加载了查询结果。
如何更改它以使查询结果显示在模式窗口中而不是显示在新页面中?
索引.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>
  $('form').submit(function(e){
  e.preventDefault() // do not submit form
      // do get request
     $.get( 'search.php', { q : },function(e){
         // then show the modal first
         $('#mymodal').modal('show');
         // then put the results there
         $('#mymodal:visible .modal-container .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>

搜索.php

<?php
  error_reporting(E_ALL);
  ini_set('display_errors',1);

  include_once('db.php'); //Connect to database
  if(isset($_POST['q'])){
    $q = $_POST['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);
?>
ohfgkhjo

ohfgkhjo1#

您可以尝试这种可能的解决方案,方法是遵循以下更改-删除表单标记-在搜索按钮上添加onclick listner-首先将内容放入modal,然后显示modal
索引.php

$(document).ready(function () {

$('.search').click(function () {
    var q= $('#q').val();

    // AJAX request
    $.ajax({
        url: 'search.php',
        type: 'post',
        data: {
            q: q
        },
        success: function (response) {
            // Add response in Modal body
            $('.modal-body').html(response);

            $('#mymodal').modal('show');
        }
    });
});
});
<head>
   <title>Search</title>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <link href='bootstrap/css/bootstrap.min.css' rel='stylesheet' type='text/css'>
   <!-- Script -->
   <script src='jquery-3.1.1.min.js' type='text/javascript'></script>
   <script src='bootstrap/js/bootstrap.min.js' type='text/javascript'></script>
</head>
<body>
   <input type="text" id="q" name="q" placeholder="Enter query"/>
   <input type="button" class="search" name="search" value="Search">
</body>
<!-- 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>

相关问题