dongyihao1099 2018-09-15 18:39
浏览 29

使用ajax搜索获取数据时不提交表单

I created a search which live search database data. I've shown data in tabular form and create a form inside this table. In this form, i need a button for form submission. The problem is, this button cannot submit form. This form is created in Fetch.php file as you can see this line of code <td><input type="submit" name="submit" value="Recover"></td>. Search is working fine but form is not submitting.

The Fetch.php code is,

    <?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';
}

?>

Search:

<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条回答 默认 最新

  • dsjpik057730 2018-09-15 18:52
    关注

    I think you break the html since the while loop adds form tags in each iteration furthermore, the all relevant inputs should be inside the form

    So 1st of all, the form should move out of the table the

    <th>action</th>
    

    should be removed

    And the while loop should remove the form tags:

    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>' ;
    

    2nd of all, the submission shohld be with the text:

    <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>
    

    Hope it helps

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效