0

I'm building a forum and I use this code to load all the comments forum_retrieve.php and this works fine at this point.

$(document).ready(function() {
  $('#results_holder').load('forum_retrieve.php<? echo ' ? forum_id = ' . urlencode($forum_id) ?>').show();
});​

But when a user submits the form to forum_handle.php (to save the comment to sql) and gets redirected back using:

if ($connect->query($sql) === TRUE) {
  header('Location: forums.php?id=' . $_POST['forum_id']);
} else {​

the Ajax wont load the new results, even if I submit 5 comments, they wont load until I refresh the page or click on another page and come back.

I have even tried putting the ajax on a time delay of 30 seconds and it still wont load the new results even though I can see it in sql before the timer is up.

I have tried using no caching headers. I have also tried $(window).ready.

Shouldn't a new query be made every time the document is loaded?I don't get why its choosing not to. Any suggestion will be greatly appreciated.

t.niese
  • 39,256
  • 9
  • 74
  • 101
mike
  • 43
  • 1
  • 7
  • How do you *know* that the Javascript is firing? Have you put some breakpoints in? Have you added some logs? Have you seen the network request firing and being received on the PHP side? – christopher Feb 25 '15 at 19:59
  • When do you ever inject the ajax results? The document.ready only fires once. – Daniel Feb 25 '15 at 20:01
  • when i set the time delay on it, it would show the old results only after the time is up.it will load all the comments just fine its only new comments the don't get loaded. – mike Feb 25 '15 at 20:04
  • so document.ready will only fire once even after multiple leaving the page and being redirected back to it? – mike Feb 25 '15 at 20:06
  • I post the form using post method then do a header redirect back to the forum page. – mike Feb 25 '15 at 20:08
  • I'm confused, in your question you said they load when the page is reloaded? Can you clarify, and provide more of the relevant code – Dan Feb 25 '15 at 20:09
  • You can't redirect in Ajax. The redirect will happen on server side and your JS callback will get the HTML result...which you can then stick in a DIV or whatever, but the location in the address bar will not change...that's the whole point of Ajax. – developerwjk Feb 25 '15 at 20:12
  • when a user submits the form using post method they then get redirected back to the forum page using "header(location: ....)" but the newly submitted comment doesn't get loaded by the javascript (the comments remain the same as before the new comment was submitted) but if I lick the browser refresh button or click on a new apge and come then the new comment will be loaded – mike Feb 25 '15 at 20:14
  • is the post being done via ajax? Then again, redirecting in Ajax responses doesn't do what you think it does. – developerwjk Feb 25 '15 at 20:17
  • @developerwjk A `Location: .... ` header won't to the redirect server side. It will still be done by the client. But yes if it is a ajax request, then the browser will do another request for the received `location: ...` and there will be no reload of the browser. – t.niese Feb 25 '15 at 20:19
  • this is how the form is submited
    – mike Feb 25 '15 at 20:19
  • @mike, If you are going to post to a page and redirect back, Ajax isn't what you want. Have you seen the pattern of posting a form to itself http://stackoverflow.com/questions/5826784/how-do-i-make-a-php-form-that-submits-to-self – Dan Feb 25 '15 at 20:19
  • ok based on everyone response I think I figure it out from hear. thanks everyone. – mike Feb 25 '15 at 20:22

1 Answers1

1

First things first: load() is not intended to be used as you imagined, not at all. It just fires a function when your $('#results_holder') is loaded. That's it. You are passing no function there, hence you get no result.

Then, as pointed out by Daniel, the load() function is fired only once, as soon as the document is ready. Then the script stops to run, so there's no chance for it to work as you want it.

What you want to do is to fetch, from time to time, the content from forum_retrieve.php and display it in a proper container.

Try something like this:

$(document).ready(function() {

    var forumID = '<?= urlencode($forum_id) ?>';

    setInterval(function() {
        $.get({
            url: 'forum_retrieve.php',
            data: {forum_id: forumID}
        })
        .done(function(data) { $('#results_holder').html(data); });
    }, 10000);
});

This fires a function every 10000ms (= 10sec) to retrieve the content from forum_retrieve.php and inject it into the $('#results_holder') element. Let me know if it works!

  • in the forum_retrieve.php im echoing html with the query veribales in it and it loads just fine. but when a user submits a comment they are taken to a php where the submition query is done, and is if the query works a "header(Location:..." redirects the user back to the forum page. so at this time shouldn't the (document)load function trigger??? I tried your code (it didn't work) but ill keep tinkering at it. – mike Feb 25 '15 at 20:58
  • I really don't understand what you're trying to do... In your question, you wrote that "the Ajax wont load the new results", what is that you're calling "Ajax"? Then, what's the point in submitting the header? Also, I've modified my code, do you want to try it again? – Alessandro Cappello Feb 25 '15 at 21:09
  • basically my question comes down to this: after the form is submitted and the user is sent to a php file that then sent back to the forum page shouldn't the $(document).ready(function() trigger at this point – mike Feb 25 '15 at 21:19
  • On my browser, $(document).ready() is fired when _my browser_ finishes to load the page. That's it. When User A submits the form and she's redirected to the forum page. Other users don't get any update simply by $(document).ready(), if that's what you're looking for. What do you expect it to do? – Alessandro Cappello Feb 25 '15 at 21:28