am-impact / amforms

Forms plugin for Craft
Other
122 stars 21 forks source link

No text after submit #150

Closed planktonWD closed 7 years ago

planktonWD commented 7 years ago

I am redirecting to the same page and showing text. When I use the default template code:

{{ craft.amForms.displayForm('contactForm') }}

The text shows fine and the form doesn't show which is to be expected.

However when I use my own template code:

{% extends "_layout" %}

{% set form = craft.amForms.getForm('contactForm') %}

{% macro errorList(errors) %}
    {% if errors %}
        <ul class="o-list-bare">
            {% for error in errors %}
                <li class="c-form-response"><em>{{ error }}</em></li>
            {% endfor %}
        </ul>
    {% endif %}
{% endmacro %}

{% from _self import errorList %}

{% block content %}

    <form class="c-form" method="post" action="" accept-charset="UTF-8">
        {{ getCsrfInput() }}

        <input type="hidden" name="action" value="amForms/submissions/saveSubmission">
        <input type="hidden" name="handle" value="{{ form.handle }}">

        {{ craft.amForms.displayAntispam() }}

        <div class="o-flex-layout">

            <div class="o-flex-layout__item u-6/13@desktop">

                <div class="u-mb o-input-container">
                    <input class="o-input" type="text" name="fields[firstName]" value="{% if contactForm.firstName is defined %}{{ contactForm.firstName }}{% endif %}" placeholder="First Name*"> 
                </div>
                {% if contactForm is defined %}
                    {{ errorList(contactForm.getErrors('firstName')) }}
                {% endif %}

            </div>

            <div class="o-flex-layout__item u-6/13@desktop u-push-1/13@desktop">

                <div class="u-mb o-input-container ">
                    <input class="o-input" type="text" name="fields[lastName]" value="{% if contactForm.lastName is defined %}{{ contactForm.lastName }}{% endif %}" placeholder="Last Name*">
                </div>
                {% if contactForm is defined %}
                    {{ errorList(contactForm.getErrors('lastName')) }}
                {% endif %}

            </div>

        </div>

        <div class="u-mb o-input-container">
            <input class="o-input" type="text" name="fields[emailAddress]" value="{% if contactForm.emailAddress is defined %}{{ contactForm.emailAddress }}{% endif %}" placeholder="Email address*">
        </div>
        {% if contactForm is defined %}
            {{ errorList(contactForm.getErrors('emailAddress')) }}
        {% endif %}    

        <div class="u-mb o-input-container o-input-container--textarea">
            <textarea class="o-input" name="fields[message]" placeholder="Message" value="{% if contactForm.message is defined %}{{ contactForm.message }}{% endif %}"></textarea>
        </div> 
        {% if contactForm is defined %}
            {{ errorList(contactForm.getErrors('message')) }}
        {% endif %}

        <p><small>* indicates a required field</small></p>

        <input type="submit" value="Submit">

    </form>

{% endblock %}

The page refreshes and the form shows. The form does save to my submissions though so seems to work just no confirmation text?

Thanks!

hubertprein commented 7 years ago

When you handle your form manually, you also need to handle the redirect and displaying text, manually. There's no way that the plugin can do this automatically for you, as it does when you call the displayForm function.

Take a look at the template that is used for the displayForm function, and you see an example of how to solve your situation.

Template path: amforms/templates/_display/templates/form.twig

planktonWD commented 7 years ago

Thanks so much @hubertprein I popped this in my template to use the default response that I put in the admin area of Craft:

{% if craft.request.getParam('submitted') == form.handle %}
        <p>{{ form.afterSubmitText|default('Thanks for your submission.'|t) }}</p>
{% else %}
...form code as in my previous comment
{% endif %}