0

This is how I post a new comment:

        if (guest.val() != '' && comment.val() != '')
        {
            $.post("events.php?action=send", $("#form").serialize() , function(data) 
            {
                $("#processing").html('');

                if ($(data).html() == 'Hello fuckign world!') 
                {
                    $("#comments").html(data);
                } 
                else 
                {
                $("#comments").html(data);
                $("#guest_name").val('');
                $("#text").val('');
                submit.fadeIn("slow");                  
                }

            });
        }
        else
        {
            $("#processing").html('');
            $(".error").fadeIn("slow");
            $(".error").html('Please fill up the fields!');

            $(".error").click(function() {
                $(".error").fadeOut("slow");
            });
            submit.fadeIn("slow");
        }
    }); 
    loadComments(); 
});

function loadComments()
{
    $.post("events.php?action=reload", { reload : 'true' } , function(callback) 
    {
        $(".comments").html('');
        $(".comments").html(callback);
    });
}

Before that, of course I have document ready, and on click function.

Now, at the end I put this:

    loadComments(); 

Which is:

function loadComments()
{
    $.post("events.php?action=reload", { reload : 'true' } , function(callback) 
    {
        $(".comments").html(callback);
    });
}

So after the jQuery finishes it's action, it will send a post again to load all results, and updatethe div that holds the data.

But for some reason , after submiting, it doesn't reload the results. I have to do F5 to reload the results..

Question:

What have I done wrong? how do I fix it? How do I reload the results using jquery/ajax?

That's how I fetch the results:

if (isset($_POST) && isset($_GET['action']) && $_GET['action'] == 'reload')
{
    $select = $CONNECT_TO_DATABASE->query("SELECT * FROM comments LIMIT 10");
    while($row = $select->fetch( PDO::FETCH_ASSOC )) {
        echo
            '
            Name: '.$row['client_name'].'<br />
            Message: '.$row['client_message'].' <br />
            Likes: '.$row['client_likes'].' <br /> <br /> <hr></hr>
            ';
    }
}
4

1 回答 1

0

修复它,我必须将 loadComments() 函数放在 click() 函数中。因此,当您执行发表评论时,您处于 click() 函数中。因此它不会在它之外做任何事情。

于 2013-04-14T20:30:59.787 回答