I am trying to create a seamless user login interaction with a modal form using ajax. Let's say I have a navigation template _navigation.html

    {% if currentUser %}
    {{ currentUser }}
    — <a href="{{ url('shop/customer') }}">{{ "My Orders"|t }}</a>
    — <a href="{{ url('shop/customer/addresses') }}">{{ "My Addresses"|t }}</a>
    — <a href="{{ url('shop/customer/cards') }}">{{ "My Cards"|t }}</a>
    — <a href="{{ logoutUrl }}">Logout</a>
    {% else %}
      <a href="" data-toggle="modal" data-target="#loginModal">{{ "Login"|t }}</a>
    — <a href="" data-toggle="modal" data-target="#registerModal">{{ "Register"|t }}</a>
    {% endif %}

After successful login I would like to refresh the view by loading the template again (using jquery.load()). Since the template is "hidden" I can't access it. If I make the template "visible" all works, but naturally I would like the template to stay "hidden".

Is there any way to achieve this?

  • I would suggest you to create a custom controller that returns the required html together with the login response. Otherwise you would have to do a dirty work around via 2 ajax requests – Robin Schambach May 20 '18 at 12:03
  • Cool, thanks. I will look into custom controllers. What do you mean with 2 ajax requests? – krizajb May 20 '18 at 12:17
  • Some people prefer to now use PHP at all. So a (pretty bad) work around would be to create a second ajax request to your template and insert the result in your container – Robin Schambach May 20 '18 at 12:27
  • Do you think "hidden" template would be accessible via ajax? – krizajb May 20 '18 at 12:31
  • When you create a custom route yes – Robin Schambach May 20 '18 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.