How do I display a success message, update the information and stay on the current page?

RichardKYA

Well-known member
Hi all,

How do I display a success message, update the information and stay on the current page?

I know using...

Code:
return $this->responseRedirect(
    XenForo_ControllerResponse_Redirect::SUCCESS,
    XenForo_Link::buildPublicLink('route', $item),
    new XenForo_Phrase('success_message_phrase')
);

...will display a message and redirect to the specified path, but how do I display a message in the same way (via the timed drop down message) but without the redirect?

Thank you for any input :)
 
Return to the same page you started from. You'll still redirect, but I think the effect would be what you're looking for.
 
Last edited:
Add
Code:
data-redirect="off"
to the form element. As long as the form element also has the class "AutoValidator" it will prevent the redirect and display the success message.
 
Unless he's looking to not refresh the data on the page (as in sending an email or some such thing) and no js is involved, wouldn't that prevent the new information from being displayed?
 
Hi guys,

Return to the same page you started from.

Unfortunately, that's not possible. I have a link on various pages that trigger the same overlay for which sends data to the same action in my controller.

As long as the form element also has the class "AutoValidator" it will prevent the redirect and display the success message.

Ah ha, I see, I had been trying data-redirect="off" but I didn't have "AutoValidator" in the form - I've added it now and it's works, but it doesn't refresh with the new data, is there any way to do both? Stay on the same page and refresh the data?

Thank you both for your replies (y)
 
Ah ha, I see, I had been trying data-redirect="off" but I didn't have "AutoValidator" in the form - I've added it now and it's works, but it doesn't refresh with the new data, is there any way to do both? Stay on the same page and refresh the data?
If you don't want to do ajax, there is a way around it.

Pass the page calling the action (say $callingPage) to the overlay template as a hidden variable.

Then parse $callingPage in the action and build a link to that page to return to.

Something like...
Code:
switch($callingPage)
{
   case 1:
     $target_url = XenForo_Link::buildPublicLink('yourapp',$item);
     break;
   case 2:
     $target_url = XenForo_Link::buildPublicLink('forum',$item);
     break;
   case 3:
     $target_url = XenForo_Link::buildPublicLink('threads',$item);
     break;
   case 4:
     $target_url = XenForo_Link::buildPublicLink('discussion',$item);
}

return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS,$target_url,new XenForo_Phrase('success_message_phrase'));

I hope that's clearer than mud. :D
 
Last edited:
Sorry for delayed reply, I was just watching those tutorials. There's some useful stuff in them, but while watching them, I thought of another way which seems a lot simpler to me.

I just added an onclick timeout to my form submit button...

Code:
<input type="submit" onclick="setTimeout('window.location.reload();', 2000);" value="{xen:phrase update}" class="button primary" />

...it works well, the data updates on the page without losing the scroll position and I've timed it to happen immediately after the XF message disappears, so it seems fluid enough for me :)
 
That will work well enough until there is some unexpected delay in processing the data (server load, etc.). Then you'll be refreshing the page before the data is processed and quite possibly before the success message is displayed.
 
Yeah, true, I was just thinking that, plus I've thought of another issue it will inevitably cause because it's available on a page that has filter options, so the information there would have to be resent too when the page gets reloaded

I think I might just remove the link lol

I wanted it for a better UX, but it seems more problematic than it's worth
 
Top Bottom