提交表单不刷新,并将mysql返回的行追加到表中

时间:2016-06-16 12:28:31

标签: 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();
?>

2 个答案:

答案 0 :(得分:0)

我很喜欢你的serch.php页面在哪里你发布数据并将响应收到你的fontend页面。 的index.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>

的search.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();
?>

答案 1 :(得分:-2)

success :  function(data) {
                $('.result').append(data);
            }

应更改为:

success :  function(data) {
                $('.result').append($.parseHTML(data));
            }