duanbo5230 2016-08-11 07:16
浏览 56
已采纳

使用Jquery在html表中附加一行?

I have a simple form that inserts data into a mysql database and displays the data from the database including the one just posted with ajax in a table. Currently the whole table gets refreshed after submitting the form but I am trying to figure how I can append a row to the table results that displays the data just submitted. Can anyone explain how I can achieve this?

The form

<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);
  • 写回答

1条回答 默认 最新

  • douyakan8924 2016-08-11 07:20
    关注

    use append() instead of html();

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

    or if you want to add new content top of table

    $('#display').prepend(newContent);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的算法代码
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据