【问题标题】:How to show data from database without refreshing the page using jquery, PHP如何在不使用 jquery、PHP 刷新页面的情况下显示数据库中的数据
【发布时间】:2017-09-29 16:29:57
【问题描述】:

是否可以在不使用 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";

                ?>

【问题讨论】:

  • jQuery load()
  • 检查“相关”侧边栏——有很多 ajax 示例。最好的办法是了解它们的工作原理,以便将相同的原则应用到您的项目中。

标签: php jquery


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    make send.php 将你需要的数据发回给你加载新插入的行。

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

    this question的第一个答案,可能有用。

    【讨论】:

    • 仅供参考,您也可以只发回一串 html 并将其附加到 dom 某处
    【解决方案3】:

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

    追加:

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

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

    【讨论】:

    • 怎么样?我对此一无所知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2012-07-03
    • 2013-12-04
    • 2017-09-20
    相关资源
    最近更新 更多