Skip Navigation

[Resolved] Accordion and Forms

This thread is resolved. Here is a description of the problem and solution.

Problem:
An ajax form is set to display a message when submitted. The problem is it is inside an accordion cell of Layouts, and when displaying the message the page reloads and the relevant accordion section is not open.

Solution:
You need to add some custom JS for when the page reloads that checks whether it is reloading because the form was submitted (checks URL parameters), and then forces a click on the appropriate accordion. In this case, we are clicking the second accordion section:

( function( $ ) {
    $( document ).ready( function(){
        let url = new URL(location.href);
        let success = url.searchParams.get("_success_message");
        
        if ( success ) {
  
          $(".panel-group .panel:nth-child(2) a").click();
        }
    });
})( jQuery );
This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by PiotrO586 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#912250

Hi,

I included a form inside Accordion, e.g.:
hidden link (Booking tab).

The form is set up to “Display a message instead of the form”, and has “Ajax submission” option enabled.

The problem is that when I submit the form, the page reloads anyway (the url is appended by “?_tt=xxx&_success_message=xxx_x&_target=xxx#cred_form_xxx_x”) and the Accordion moves to the first tab, instead of showing the Booking tab (the second tab), which is not expected as user should see a message being shown upon form submission. Is there any workaround to keep displaying the tab which has the form?

#912417

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You are right that displaying a success message with a Form involves a page redirect even if submitting the form via ajax, so the status of the accordions is lost.

What you can do is check whether one of the URL parameters is presented in the page URL to know whether the redirect for the message has occurred, and, if so, force a click on the link to open the appropriate accordion.

I did the following on a test site.

I created my form with the settings to display the message after submitting via ajax.

I inserted this form on a standard page.

Because I want to add some custom JS to just this page I created a Content Template that is not assigned to any post type which includes just the post body and nothing else.

In the custom JS section I then added the following code:

( function( $ ) {
	$( document ).ready( function(){
		let url = new URL(location.href);
		let success = url.searchParams.get("_success_message");
      
		if ( success ) {

          $("a[href='#collapseTwo']").click();
        }
	});
})( jQuery );

I then edited the page that contains the form and assigned this specific Content Template to that page alone.

I then tested submitting the form, and after the page reloads the correct accordion is opened.

Note with the code above you would need to edit the selector for the link to click.

In my testing I simply copied and pasted the Bootstrap doc markup for the accordions, and so my second accordion has an id of "collapseTwo". You'll need to edit that for your site depending on your accordion implementation.

#912590
screen.png

Hi Nigel,

In my case, the accordion is placed inside layout, which is assigned to custom post. I placed the code in Layouts CSS and JS Editor.

Anyway, the part I cannot figure out for now is how you assign ID to the second accordion tab? I tried to placed ID in the Edit accordion panel screen (screen), but no luck.

#913025

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I couldn't see any way to helpfully add an ID or class where required.

Your booking tab is the second tab, so I think you can use an alternative selector to target the required link, like so:

( function( $ ) {
    $( document ).ready( function(){
        let url = new URL(location.href);
        let success = url.searchParams.get("_success_message");
       
        if ( success ) {
 
          $(".panel-group .panel:nth-child(2) a").click();
        }
    });
})( jQuery );
#913547

Nigel, great stuff, that works. Thanks.