Where to return to after Commenting?

On my website, beneath each Article is a Comments section where users can post messages.

The layout looks very similar to how Threads and Posts exist on SitePoint.

In addition, when Comments exceed a set limit, pagination kicks in. And the user also has the ability to filter and sort Comments.

Finally, after a user submits a Comment, they are taken to a Confirmation Page, and have a button they can click to return to the Article.

Question: Where should I take the user after he/she posts a Comment?

It isn’t as easy as it sounds, because of the Pagination and Filtering and Sorting.

Here are some choices…

1.) Return to the top of the Article

2.) Return to the top of the Comments section

3.) Clear out the user’s Filtering & Sorting preferences, switch to “View All Comments” mode, and take the user to the page bottom with the idea being that the user will see his/her newly posted Comment

4.) Other?

A lot of people might choose #3, but I’m not so crazy about erasing the user’s Filtering & Sorting & Pagination choices. In addition, I don’t want to go out of my way to display “All Comments”, because if there are 2,000 Comments, then that is a lot of bandwidth that is probably unnecessary in the scheme of things?! :eek:




What kind of filtering and sorting options do you have? Isn’t it possible for each of those to calculate where their comment would be and show it, while keeping the filtering and sorting? That would be my preference.

I’d also do away with the confirmation page. Confirmation pages are one of the most annoying thing of the internet, unless it really is the end of some sort of process like a questionnaire.
Instead you could use so called flash messages. Those are messages that appear just one time when a page loads, but are gone the next page load. The basic principle goes something like this:
(you do need sessions for this, but I’m pretty sure you already use those anyway, right?)

Somewhere in your main functions file

function add_flash($message, $type) {
    $flashes = isset($_SESSION['flashes']) ? $_SESSION['flashes'] : array();
    $flashes[] = array('message' => $message, 'type' => $type);
    $_SESSION['flashes'] = $flashes;

function has_flashes() {
    $flashes = $_SESSION['flashes'];
    return count($flashes) > 0;

function get_flashes() {
    if (!isset($_SESSION['flashes'])) {
        return array();
    $flashes = $_SESSION['flashes'];
    // unset $_SESSION['flashes'] so next time this function is called it will return an empty array
    // ensuring we only show each flash exactly once
    return $flashes;

And then in your file that receives the comment

    // check input and save comment
    add_flash('Your comment was saved!', 'success');

And then in the template

if (has_flashes()) {
    foreach (get_flashes() as $flash) {
        echo '<div class="flash flash-"', $flash['type'], '">', $flash['message'], '</div>';

Basic idea for css:

.flash {
    margin-bottom: 10px;
    border-radius: 4px;
    padding: 4px 10px;
    color: #000;
    border: 1px solid #000;
.flash-success {
    background: #99FF99;
    border-color: #0f0;
.flash-error {
    background: #FF8484;
    border-color: #f00;


Of course this is not something you have to do, just something you could do :slight_smile:

Again there’s no right or wrong way of approaching this.

More than not when ever I post a comment on a site I get redirected to the bottom of the comments list which either displays my comment, or if comments are moderated a placeholder saying “Thanks for your comment, it will appear here soon…”. many forums work this way as well.

That said my current way of thinking is that once someone has posted a comment they are most likely finished with that particular article so don’t really want to be on that page any more. So what I have done on a few sites is redirect to the top of the article and insert a simply confirmation message there along with an anchor link to my comment if I want to see it.

Not sure what you mean. Beneath each article, you can filter comments on things like “Editor’s Choice” and sort comments by things like “Date” and “Most Popular”.

Possible, but more difficult. (This is why my website never gets done, Remon!) :stuck_out_tongue:

I’m looking into this now, because in the end, it makes most sense to return someone to where their comment would be.

Then you will NOT like my website. sigh

Off Topic:

I use Confirmation Pages generously on my site, but I don’t see that as a bad thing. I like them. (If this approach was indeed a bad choice in the eyes of others, then I have a boatload of work in v3.0!!) :eek:

Whenever there is an error, I display a pretty Error Page. And when people finish things like Updating a Password, or Updating their Profile, or Adding a Friend, or Posting a Comment, I display a pretty Outcome Page.

I suppose I can see not needing to display a “Thanks for your comments, Remon. Once approved your comments will appear below.” message if people post a lot of comments, like you post on SitePoint, but for most things on my site, I like m “Outcome Page”.

I guess time will tell if that is a good or bad thing?!

Still trying to get me to use JavaScript, huh? :lol:

Yeah, I’m not going down that path for sure.

(Maybe if I build a mobile version of my site, then I’ll try to be more “hip” and catch up with the times, but for now, I think what I have works…)

I appreciate you challenging me. It is usually a good thing, and I am GRATEFUL for your suggestions about using Query Strings a few weeks ago. (I took your advice and applied everything you suggested!) :tup:

I sure hope people like my website when it is done, because I have been pouring my heart and soul into this thing!!



Sounds like a good case for A/B testing:

A: People see a confirmation screen and have to browse away manually from there
B: People get redirected to the page they came from with an extra message on there

And then check how many people browse on from A and how many people browse on from B :wink:

Who said anything about javascript? The code I showed is PHP and PHP only.

Glad I could help :slight_smile:


After looking at things, this is what I have discovered…

If you were here…


…when you clicked on the “Add a Comment” button, then when you click on “Return to Article” on my outcome page, I can easily take you back to where you were.

This is good if you were on the last page of the series (e.g. Page 4 of 4).

However, what if there are 7 pages of Comments, and the user is on Page 3 and they click “Add a Comment”?

Yes, in an ideal world, I would take then to the last page (i.e. Page 7), but that is much harder for me to do currently.

So, do I need to account for such behavior?

Or do I just return people to wherever they were at, and if the page you were formerly on, is not the last page of Comments, then you’ll just have to navigate there on your own?! :-/



How come? It’s not a hard problem. If they are sorting descending you send them to page 1, and if they’re sorting ascending you send them to page floor($totalNumberOfCommentsForThisArticle / $numberOfCommentsPerPage).

Ideally you would take it into account. Otherwise people might be put off by the fact that they just posted something and it isn’t showing. Then again, they might not be. Probably also depends how tech savvy your audience is.

The really hard thing -in my opinion- is filters like “editor’s picks”, because if I’ve just posted something chances are great (if not 100%) it’s not an editor’s pick at the moment I return to the comments, so it won’t be listed at all using the current settings. What do you show then? Maybe a message “Your comment has been posted, but due to current filter settings it is not visible at this moment”? Not really hard to build, but it might confuse the cr*p out of people.

It is complicated, because a user is on “article.php” and has filtering and sorting chosen, and when they add a comment they go to “add_comment.php” and so I have to pass all of that data to my other script. Not to mention, what if I have a “pretty URL” with no query-string like this…


Now I have to pass the default values to my “add_comment.php” script?!

You missed my point…

If a user is here…


…and they post a comment, but the “last page” is page 7, then where should I take them?

Presumably I would keep the same filtering and sorting, but instead of taking them literally back to where they were, I would take them to Page 7.

Well, during supper, this is the approach I came up with…

If you were in your Inbox (e.g. email or PM) and you had things sorted by Subject in ascending order, and you were on page 2 on Message #27, and then you clicked on the column heading to sort in descending order by Date, most UI’s would take you back to the top of Page 1. (It is understood that if you re-sort, you lose your place!)

Well, thinking things over, I think the same should apply here!

Regardless of what filter or sort you have chosen, or what page you are on, if you add a new Comment, then I think it is quite sufficient to set Filter=None, Sort=Date, Order=Asc and take you to the last page so you can see your new Comment.

First off, you won’t be “Editor’s Choice” or “Most Popular” in the few seconds from your post, so Filtering is out the window.

And if you had Comments sorted by “Rating” or even “Date”, then get over it.

Things sorted in the default way - which is oldest Comment 1st, and newest Comment last - and having my code take you to whatever is the last page and to the bottom of it so you can see your new Comment is more than enough!

I think I am saying something similar to what you said above, but I disagree that if I left everything the way it was and tried to take the user back to the same Filter, Sort, Order, Page that it would be easy going between two scripts! (Doable? Yes. Easy? No! Going to happen? Nope!)




Yeah, I think clearing all filters and sorting options is fair enough :slight_smile:


(Anyone care to 2nd ScallioXTX’s thoughts?) :slight_smile:



I think the best choice is to get back to the user’s newly posted comment. After posting anything, everyone wants to see that it is posted or not? So that it is good to return to the newly posted comment section.