1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'XenForo Development Discussions' started by RichardKYA, Jun 28, 2016.

  1. RichardKYA

    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 :)
     
  2. Snog

    Snog Well-Known Member

    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: Jun 28, 2016
  3. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
    Snog likes this.
  4. Snog

    Snog Well-Known Member

    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?
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    Snog likes this.
  6. RichardKYA

    RichardKYA Well-Known Member

    Hi guys,

    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.

    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)
     
  7. RichardKYA

    RichardKYA Well-Known Member

    I will take a look at those tuts :)

    Thanks again guys (y)
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    Yeah I think it's those tutorials you need.
     
  9. Snog

    Snog Well-Known Member

    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: Jun 28, 2016
  10. RichardKYA

    RichardKYA Well-Known Member

    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 :)
     
  11. Snog

    Snog Well-Known Member

    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.
     
  12. RichardKYA

    RichardKYA Well-Known Member

    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
     

Share This Page