3

I am having trouble getting a freeform success message to display. I am posting back to the same template. What is the proper way to check if the form was posted? I see in the freeform render template there is a freeform.hasErrors, is there something for success perhaps?

            {% if craft.request.getPost('firstName')|length %}

                <div class="callout success">Your message has been sent.</div>

            {% else %}

                {{ craft.freeform.form("contactForm", {
                        formAttributes: { "data-abide": true }, 
                        instructionsBelowField: true, 
                        submitClass: "button", 
                        returnUrl: "/contact", 
                        useRequiredAttribute: true
                    }
                ).render() }}

            {% endif %}

https://solspace.com/craft/freeform/docs

Update: Working code using query param instead. Thanks @Jan_dh.

            {% if craft.request.getQuery('success') == 1 %}

                <div class="callout success">Your message has been sent.</div>

            {% endif %}

            {{ craft.freeform.form("contactForm", {
                    formAttributes: { "data-abide": true }, 
                    instructionsBelowField: true, 
                    submitClass: "button", 
                    returnUrl: "/contact?success=1", 
                    useRequiredAttribute: true
                }
            ).render() }}

            {% endif %}
5

You can add a param to your returnUrl in the form composer then check the template for the param.

For example:

You redirect to /contact?success

{% set successParam = craft.request.getParam('success') %}
{% if successParam is not null %} 
  <p>Hooray! Success!!</p>
{% endif %}

Another option is to Ajax submit your form and just have a modal or something on a succes submit. An example of an Ajax submit can be found in the official freeform docs here.

| improve this answer | |
  • Thanks. I tried that, and setup a route to use my same contact template and it displayed the success message...the problem with that route is that contact is a single in Craft. So if i access it at contact/success it doesn't have the {{ entry }} fields. Am I doing something wrong? I want to use the same template for contact and contact/success. Perhaps i do need to go the ajax route. – Shift2Design Jan 29 '17 at 18:57
  • Instead of returning a new segment you can return a param. I'll edit my answer – Jan_dh Jan 29 '17 at 19:03
  • Great idea. It's fixed. I don't post here often, what is the best way to post the working code? – Shift2Design Jan 29 '17 at 19:10
  • You can edit your original question with a little note that that is the working code. Welcome to the Craft family ;) – Jan_dh Jan 29 '17 at 19:14
  • I updated original question with some working code. Thank you. – Shift2Design Jan 29 '17 at 19:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.