0

无论如何都可以在不使用 jQuery 刷新页面的情况下将新插入的行加载到数据库中?我能够将数据发送到数据库而无需使用 jquery 刷新,但我坚持在不刷新页面的情况下显示该数据。如何在不刷新页面的情况下显示表格中的数据并使其显示在 index.php 中的表格下方,我必须显示检索到的数据?谢谢

这是我的 index.php

<html>
        <head>
            <script src="jquery.js" type="text/javascript"></script>
            <script type="text/javascript">
            $(document).ready(function(){
            $('#submit').click(function(){
            $.ajax({
            type: "POST",

            url: 'send.php',
            data: "user=" + $('#user').val() + "&comment=" + $('#comment').val(),
            success: function(data){

            $('input[type=text]').val('')
            $('#status').html(data);

            }

            });
            });
            });
            </script>
                            </head>
                     <body>
                      <div>
        <input type="text" name="user" id="user">
        <input type="text" name="comment" id="comment">
        <input name="submit"  type="submit" id="submit">

        <div id="status"></diV>
        </div>
        <br>


        <?php
       $con=mysqli_connect("localhost","root","","user");
        // Check connection
        if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

        $result = mysqli_query($con,"SELECT * FROM comments");

    echo "<table  width='640' >
    <tr>

    </tr>";

    while($row = mysqli_fetch_array($result))
      {
      echo "<tr >";
      echo "<td  style='vertical-align:text-top'>" . $row['user'] . "</td>";

      echo "<td><br><br><br>" . wordwrap($row['comment'], 90, '<br>',true) . "</td>";
      echo "</tr>";
      }
    echo "</table>";

    mysqli_close($con);
    ?> 

这是我的 send.php,它将数据提交到表中。

<?php
                $mysqli = new mysqli("localhost", "root", "", "user");
                    if ($mysqli->connect_errno) {
                    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
                }

                $username = $_POST["user"];
                $comment = nl2br($_POST['comment']);
                $stmt = $mysqli->prepare("INSERT INTO comments (user, comment) VALUES (?,?)");
                $stmt->bind_param('ss', $username, $comment);
                $stmt->execute();

                echo"comment posted successfully";

                ?>
4

3 回答 3

2

这是一个使用 JQuery AJAX 和 php 从 mysql 数据库中获取数据的简短示例。JQuery AJAX 允许我们在不重新加载页面的情况下更新页面的内容:

http://openenergymonitor.org/emon/node/107

http://viralpatel.net/blogs/jquery-ajax-tutorial-example-ajax-jquery-development/

于 2013-06-21T23:03:02.470 回答
0

使 send.php 将加载新插入的行所需的数据发回给您。

假设您插入了一条“Hello world”的评论。例如,如果没有任何问题,send.php 可以回显评论的内容(在本例中为 Hello world),您可以在成功函数中使用它(来自 data 参数)。

看看这个问题的第一个答案,可能会有用。

于 2013-06-21T23:05:03.767 回答
0

您应该在 ajax 请求的成功回调函数中使用 jquery 将新注释附加到表中。

追加:

$("table").append("<tr><td style='vertical-align:text-top'>"+$('#user').val()+"</td><td><br><br><br>"+$('#comment').val()+"</td></tr>");

将要附加的内容保留在一行代码中,并确保在将 #user 和 #comment 的值包含到附加字符串之前不清除它们。

于 2013-06-21T22:51:38.017 回答