javascript - 提交表单,无需刷新并将 mysql 返回的行追加到表中

标签 javascript php jquery html ajax

我似乎无法正确地将服务器返回的所有行附加到表的主体中。这是我的 ajax 代码。 HTML 就在下面。就像我可以看到返回的响应一样,它不仅仅附加到我现有的表中。有人可以帮我解决这个问题吗?我已经尝试了几个小时,今天我必须向我的老板演示它

<script type="text/javascript">
$(document).ready(function() {
    $(document).on('submit', '#formID', function() {
        var data = $(this).serialize();
        $.ajax({
            type : 'POST',
            url  : 'search.php',
            data : data,
            success :  function(data) {
                $('.result').append(data);
            }
        });
        return false;
    });
});
</script>

HTML

<table id="example3" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>***</th>
            <th>Description</th>
            <th>**</th>
            <th>Source</th>
            <th></th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>***</th>
            <th>Description</th>
            <th>**</th>
            <th>Source</th>
            <th></th>
        </tr>
    </tfoot>
    <tbody class="result">

    </tbody>
</table>


<?php


$servername = "*****";
$username = "*****";
$password = "*****";
$dbname = "*****";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


    $fields = array('*****', '*****', '*****', '*****', '*****');

    $conditions = array();

  foreach($fields as $field){
        // if the field is set and not empty
        if(isset($_POST[$field]) && $_POST[$field] != '') {
            // create a new condition while escaping the value inputed by the user (SQL Injection)
            $conditions[] = "`$field` LIKE '%" . mysqli_real_escape_string($conn,$_POST[$field]) . "%'";
        }
    }


$query = " SELECT * FROM $dbname.***** P ";

            if(count($conditions) >0) {

            $query .= "WHERE " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
    }               

    $result = mysqli_query($conn,$query);

if (!$result) {
    echo "ERRORS";
}

    while($row = $result->fetch_assoc())  {
         echo "<tr><td>" .$row['*****'] . "</td><td>*****</td><td>September 12 1993</td><td>*****</td><td>May 24 1998</td><td>May 24 2005</td><td>*****</td><td>*****</td> </tr>";                       

     }



$conn->close();
?>

最佳答案

我想知道你的 serch.php 页面在哪里,你可以在其中发布数据并将响应放入你的 fontend 页面。 索引.php

<table id="example3" class="display" cellspacing="0" width="100%">
<thead>
    <tr>
        <th>***</th>
        <th>Description</th>
        <th>**</th>
        <th>Source</th>
        <th></th>
    </tr>
</thead>
<tfoot>
    <tr>
        <th>***</th>
        <th>Description</th>
        <th>**</th>
        <th>Source</th>
        <th></th>
    </tr>
</tfoot>
<tbody class="result">

</tbody>

<script type="text/javascript">
$(document).ready(function() {
$(document).on('submit', '#formID', function() {
    var data = $(this).serialize();
    $.ajax({
        type : 'POST',
        url  : 'search.php',
        data : data,
        success :  function(data) {
            $('.result').append(data);
        }
    });
    return false;
 });
 });
</script>

搜索.php

<?php
 $servername = "*****";
 $username = "*****";
 $password = "*****";
 $dbname = "*****";
 $conn = new mysqli($servername, $username, $password, $dbname);
 if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
  }
$fields = array('*****', '*****', '*****', '*****', '*****');

$conditions = array();
foreach($fields as $field){
    // if the field is set and not empty
    if(isset($_POST[$field]) && $_POST[$field] != '') {
        // create a new condition while escaping the value inputed by the user (SQL Injection)
        $conditions[] = "`$field` LIKE '%" . mysqli_real_escape_string($conn,$_POST[$field]) . "%'";
    }
}
$query = " SELECT * FROM $dbname.***** P ";

        if(count($conditions) >0) {

        $query .= "WHERE " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}               

$result = mysqli_query($conn,$query);

 if (!$result) {
    echo "ERRORS";
 }

while($row = $result->fetch_assoc())  {
     echo "<tr><td>" .$row['*****'] . "</td><td>*****</td><td>September 12 1993</td><td>*****</td><td>May 24 1998</td><td>May 24 2005</td><td>*****</td><td>*****</td> </tr>";                       

 }
 $conn->close();
?>

关于javascript - 提交表单,无需刷新并将 mysql 返回的行追加到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37859257/

相关文章:

javascript - 在鼠标悬停时打开一个 div 并保持打开状态直到鼠标移出?

javascript - jQuery 函数在第一次单击时部分触发,在第二次单击时起作用

javascript - Strage Vue 行为。 V-if 没有正常观看

javascript - 如何从另一个方法中正确调用类方法

php - 分离 Drupal 模块逻辑和 UI

java - 使用 cookie 从 PHP 读取 JSON

javascript - 当分配的变量发生变化时,React 状态也会发生变化

php - MySQL HY000 1366 : Trying to store 4 byte utf8 character

javascript - Jquery 根据数字内容更改对象的 CSS 类

javascript - 按钮单击功能在 jQuery 中不起作用