用Jquery向html表添加一行


Append a row to an html table with Jquery?

我有一个简单的表单,插入数据到mysql数据库中,并显示来自数据库的数据,包括刚刚在表中使用ajax发布的数据。目前,整个表得到刷新后提交的形式,但我试图找出如何我可以追加一行表的结果,显示刚刚提交的数据。有人能解释一下我是怎么做到的吗?

的形式
<div class="form-group">
    <form class="" action="" id="post_data" method="post">
        <label>
                Post Data
              </label>
        <input id="input" name="input" type="text" class="form-control input-md">
</div>
<button type="submit" class="btn pull-right btn-primary" id="submit">
              Save
            </button>
</form>
<div class="display" id="display"></div>
</div>
<script>
    $(document).ready(function() {
        $('#post_data').submit(function() {
            $.ajax({
                url: 'process.php',
                type: 'POST',
                dataType: 'html',
                data: $(this).serialize(),
                success: function(newContent) {
                    $('#display').html(newContent);
                }
            });
            return false;
        });
    });
</script>

Process.php

$stmt = $db_conx->prepare('INSERT INTO ajax_test (input) VALUES (?)');
$stmt->bind_param('s', $input);
$stmt->execute();
$sql = "SELECT input FROM ajax_test";
$result = mysqli_query($mysqli, $sql);

while ($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $id . "</td>";
    echo "<td>" . $row["input"] . "</td>";
    echo "</tr>";
}
}
mysqli_close($mysqli);

使用append()代替html();

$('#display').append(newContent);

或者如果你想添加新的内容

$('#display').prepend(newContent);