XF 2.3 Redirecting a form with a confirmation message

Lee

Well-known member
I have the following method:

PHP:
    public function actionToggleTrigger()
    {
        $this->assertPostOnly();
   
        $visitor = \XF::visitor();
        if (!$visitor->user_id) {
            return $this->noPermission();
        }
   
        $profile = $visitor->Profile;
        $current = $profile->custom_fields->ToggleTrigger ?? '0';
        $new = $current === '1' ? '0' : '1';
   
        $profile->custom_fields->ToggleTrigger = $new;
        $profile->save();

        $confirmation = \XF::phrase('trigger_settings_toggled');
        return $this->redirect($this->buildLink('index'), $confirmation);

    }

and the following template code:

HTML:
<xf:form action="{{ link('account/toggle-trigger') }}"
    method="post"
    data-xf-init="ajax"
    data-skip-overlay-redirect="true"
    class="ToggleTrigger">

    <xf:submitrow submit="Toggle Setting" rowtype="simple" />
</xf:form>

This works and it toggles the custom profile field as expected and redirects back to the index.

I have two questions:

1) How do I capture the URL the current user is on and redirect back to that
2) How do I update it with AJAX so the user doesn't actually leave the page and flash a confirmation message?

I feel like I am missing something obvious.
 
Last edited:
You can capture the URL via \XF\Mvc\Controller::getDynamicRedirect in your controller action. You can add data-redirect="off" to <xf:form> to not redirect the page when submitted over AJAX.
 
Back
Top Bottom