使用Ajax搜索获取数据时未提交表单

时间:2018-09-15 18:39:09

标签: php mysql

我创建了一个实时搜索数据库数据的搜索。我已经以表格形式显示了数据,并在此表中创建了一个表格。在此表单中,我需要一个按钮来提交表单。问题是,此按钮无法提交表单。在Fetch.php文件中创建了此表单,您可以看到此行代码<td><input type="submit" name="submit" value="Recover"></td>。搜索工作正常,但表单未提交。

Fetch.php代码是

    <?php
$connect = mysqli_connect("localhost", "root", "", "stolen_cars");
$output = '';
if(isset($_POST["query"]))
{
 $search = mysqli_real_escape_string($connect, $_POST["query"]);
 $query = "
  SELECT * FROM record 
  WHERE name LIKE '%".$search."%'
  OR model LIKE '%".$search."%' 
  OR registration_no LIKE '%".$search."%' 
  OR chassis_no LIKE '%".$search."%' 
  OR color LIKE '%".$search."%'
  OR city LIKE '%".$search."%'
  OR district LIKE '%".$search."%'
 ";
}
else
{
 $query = "
  SELECT * FROM record ORDER BY id
 ";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
 $output .= '
  <div class="table-responsive">
   <table class="table table bordered">
    <tr>
     <th>Name</th>
     <th>Model</th>
     <th>Reg.#</th>
     <th>Chassis#</th>
     <th>Engine#</th>
     <th>Color</th>
     <th>City</th>
     <th>Distt.</th>
     <th>Action</th>
    </tr>
 ';
 $output .= '<form method="post" action="index.php" class=text-center>' ;
 while($row = mysqli_fetch_array($result))
 {
  $output .= '

   <tr>
    <td>'.$row["name"].'</td>
    <td>'.$row["model"].'</td>
    <td>'.$row["registration_no"].'</td>
    <td>'.$row["chassis_no"].'</td>
    <td>'.$row["engine_no"].'</td>
    <td>'.$row["color"].'</td>
    <td>'.$row["city"].'</td>
    <td>'.$row["district"].'</td>
    <td><input type="submit" name="submit" value="Recover"></td>
   </tr>
  ';
 }
$output .= '</form>';
 echo $output;
 }

else
{
 echo 'Data Not Found';
}

?>

搜索:

<div class="container">
        <div class="input-group mb-3">
            <div class="input-group-prepend">
            <span class="input-group-text" id="basic-addon1">Search</span>
            </div>
            <input type="text" name="search_text" id="search_text" placeholder="Search by name, model, registeration#, chassis# etc." class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
        </div>
        <div id="result"></div>
  </div>
<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>

2 个答案:

答案 0 :(得分:0)

我认为您会破坏html,因为while循环在每次迭代中都会添加表单标签 此外,所有相关输入都应在表格内

首先,表格应移出表格

<th>action</th>

应删除

而while循环应删除表单标签:

while($row = mysqli_fetch_array($result))
 {
  $output .= '

   <tr>
    <td>'.$row["name"].'</td>
    <td>'.$row["model"].'</td>
    <td>'.$row["registration_no"].'</td>
    <td>'.$row["chassis_no"].'</td>
    <td>'.$row["engine_no"].'</td>
    <td>'.$row["color"].'</td>
    <td>'.$row["city"].'</td>
    <td>'.$row["district"].'</td>
   </tr>
  ';
 }
$output .=' </table>' ;

第二,提交的内容应为文字:

<form method="post" action="index.php" class=text-center>' ;
<input type="text" name="search_text" id="search_text" placeholder="Search by name, model, registeration#, chassis# etc." class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
<input type="submit" name="submit" value="Recover">
</form>

希望有帮助

答案 1 :(得分:0)

ID %in% as.vector(unlist(res))