获取在php url上创建的varibles的值

时间:2017-09-27 16:36:27

标签: php ajax

我想回来并在我的index.php上使用将在load-comments.php上创建的commentNewCount的值。我怎么能这样做?

 <script>   
    $(document).ready(function() {
        var commentCount = 2;
        $("button").click(function() {
            commentCount = commentCount + 2;
            $("#comments").load("load-comments.php", {
                commentNewCount: commentCount
            });
        });
    });
    </script>

负载的comments.php

    <?php
    include 'dbh.php';

    $commentNewCount = $_POST['commentNewCount'];

    $sql = "SELECT * FROM comments LIMIT $commentNewCount";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<p>";
            echo $row['author'];
            echo "<br>";
            echo $row['message'];
            echo "</p>";
        }
    } else {
        echo "There are no comments!";
    }
?>

1 个答案:

答案 0 :(得分:0)

如果您只是想将变量从一个页面传递到另一个页面,那么您可以将commentNewCount的值保存到SESSION变量或COOKIE。

//on load-comments.php
$_SESSION['commentCount'] = $_POST['commentNewCount'];

//on index.php
$commentCount = $_SESSION['commentCount'];