Posted
If you use the ChronoForms plug-in or module to display a form then, by default, when the form is submitted the same page will be re-displayed and, sometimes, the form will be displayed again instead of the thank you message.
ChronoForms uses the current page url as the form action url and re-loads it after submission with the added parameter &event=submit which tells the form to display the Thank You message. However, sometimes the site SEF URL component will remove this parameter and when that happens the form may be redisplayed.
You can avoid this by using a Custom Code action in the form On Submit event to store a token in the User Session and then check for this when the page loads. If no token is found you display the form, if a token is found then show a thank you message.
Here's the code to add a token to the User Session, put this in a Custom Code action in the On Submit event:
<?php $session =& JFactory::getSession(); $form_id =& $form->form_details->id; $session->set( 'cf_submitted_'.$form_id, '1' ); ?>
To check this use a Custom Serverside Validation action in the On Load event where you add this code:
<?php $session =& JFactory::getSession(); $form_id =& $form->form_details->id; $check = $session->get( 'cf_submitted_'.$form_id, '0' ); if ( $check ) { return false; } ?>
Then add a Show Thanks Message and a Show Stopper action to the On Fail event of the Custom Serverside Validation action.
Now if the page is re-loaded the Thank You message from the On Fail event will be displayed. (Note: if the SEF URLs allow it then the normal Thank You message will display the first time that the form is submitted.)
Having the Thanks Message scroll into view
User OzNeilAu suggests that you can use JavaScript to have the page scroll to show a form thanks message if the form is not at the top of the page.
You need to add an id to the Thanks Message like this:
<div id='form_thanks_message' >
Thank you . . . // your message goes here
</div>
And, in Joomla! v1 or v2.5 add a Load JS action to the form On Submit event with code like this in it:
window.addEvent('domready', function() { $('form_thanks_message ').scrollIntoView(); });
And for Joomla! 3 using jQuery (v5):
jQuery(document).ready(function(jQ){ jQ('#form_thanks_message')[0].scrollIntoView(); });
You can use any id for the div provided that you use the same id in the JavaScript.
Please also see this FAQ for ways to scroll to the form when there is a Captcha or Serverside validation error on a form.
Showing a Joomla! System Message
Provided that your template supports Joomla! system messages you can use a Custom Code action to show one at the top of the page.
<?php $app = \JFactory::getApplication(); $app->enqueueMessage('Your message here'); ?>