Search code examples
phpjqueryajaxrequestforum

PHP/JQuery - Redirect page into an AJAX request


I've designed a forum on my website that operates through AJAX requests therefore the URL does not change at all. You click a button, the AJAX requests a PHP file, and the response updates the client. This works for me - however, I need to know if it's possible to redirect a user through a series of AJAX requests after a form submission.

  1. User enters forum.php page
  2. User clicks on topic button (AJAX request to load threads)
  3. User clicks on thread title (AJAX request to load replies)
  4. User clicks on reply button (AJAX request to load text editor)
  5. User clicks on submit (AJAX request to submit form)

At this point I want to redirect the user back to that specific thread - therefore I believe they would need to be directed through a series of AJAX requests. Is this possible?

Extra: Also if you believe this forum process is not efficient please let me know your opinions.


Solution

  • Seems you have a few options:

    1. continue to use Ajax for the submit, so no need to redirect. (EDIT: sounds like you are already doing this, so the problem is probably you are discarding the content you want to display, rather than just hiding it temporarily)
    2. implement a way to load a page directly to the forum. This would be in addition to the ajax call. You would track the forum ID, include it in $_POST parameters, and have the server redirect to the appropriate URL.
    3. Generate javascript (or approriate fields/data for your fixed javascript to use) that will cause the page, when reloaded, to automatically make the necessary AJAX calls.
    4. Skip Ajax and just use full page loads for everything

    Mixing things up, by sometimes using Ajax and sometimes using a page load isn't ideal, but it is sometimes difficult to avoid completely.

    Keep in mind what happens when user hits the BACK button or history in the browser. Do you want them to leave your app when hitting back? They can end up in a weird place mixing ajax/page loads. For this reason, people favor (4)

    (2) is probably a lot of work.

    (1) would be ok, and BACK button will just always take the user WAY BACK to some other website. Personally, that wouldn't bother me.

    (3) would be a pain and also be slow, and then you get pretty weird forward/back behavior. Avoid.

    (4) might just be best.