How to get contact from to disappear when submitted successfully?

How to get contact from to disappear when submitted successfully?

hjth96
Excursionist
17 1 1

Hi all, 

I'm doing something a little different with my website. When someone submits a contact form, I would like an image and success message to pop up and the actual contact form to disappear. I've already got half of this with the image and success message popping up, but I cannot figure out the code to make the form disappear. I would also like to add a button that redirects them to the contact page if they have another question. I've attached the code as well as an image of what appears when a message is successfully submitted. Please let me know how can I edit this code to make this happen. Thanks!Screenshot 2025-03-12 142747.png

{{ 'section-contact-form.css' | asset_url | stylesheet_tag }}

{%- style -%}
  .section-{{ section.id }}-padding {
    padding-top: {{ section.settings.padding_top | times: 0.75 | round: 0 }}px;
    padding-bottom: {{ section.settings.padding_bottom | times: 0.75 | round: 0 }}px;
  }

  @media screen and (min-width: 750px) {
    .section-{{ section.id }}-padding {
      padding-top: {{ section.settings.padding_top }}px;
      padding-bottom: {{ section.settings.padding_bottom }}px;
    }
  }
{%- endstyle -%}

<div class="color-{{ section.settings.color_scheme }} gradient">
  <div class="contact page-width page-width--narrow section-{{ section.id }}-padding">
    {%- if section.settings.heading != blank -%}
      <h2 class="title title-wrapper--no-top-margin inline-richtext {{ section.settings.heading_size }}{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--slide-in{% endif %}">
        {{ section.settings.heading }}
      </h2>
    {%- else -%}
      <h2 class="visually-hidden">{{ 'templates.contact.form.title' | t }}</h2>
    {%- endif -%}
    {%- liquid
      assign contact_form_class = 'isolate'
      if settings.animations_reveal_on_scroll
        assign contact_form_class = 'isolate scroll-trigger animate--slide-in'
      endif
    -%}
    
  {%- form 'contact', id: 'ContactForm', class: contact_form_class -%}
  {%- if form.posted_successfully? -%}
    <h2 class="form-status form-status-list form__message" tabindex="-1" autofocus>
      {{- 'icon-success.svg' | inline_asset_content -}}
      {{ 'templates.contact.form.post_success' | t }}
    </h2>
    <div style="text-align: center;">
      <img src="https://cdn.shopify.com/s/files/1/0896/4807/1021/files/Notes.png?v=1741802045" alt="Form submitted successfully" style="width: 300px; height: auto;" class="success-image">
    </div>


        </h2>
      {%- elsif form.errors -%}
        <div class="form__message">
          <h2 class="form-status caption-large text-body" role="alert" tabindex="-1" autofocus>
            {{- 'icon-error.svg' | inline_asset_content -}}
            {{ 'templates.contact.form.error_heading' | t }}
          </h2>
        </div>
        <ul class="form-status-list caption-large" role="list">
          <li>
            <a href="#ContactForm-email" class="link">
              {{ form.errors.translated_fields.email | capitalize }}
              {{ form.errors.messages.email }}
            </a>
          </li>
        </ul>
      {%- endif -%}
      <div class="contact__fields">
        <div class="field">
          <input
            class="field__input"
            autocomplete="name"
            type="text"
            id="ContactForm-name"
            name="contact[{{ 'templates.contact.form.name' | t }}]"
            value="{% if form.name %}{{ form.name }}{% elsif customer %}{{ customer.name }}{% endif %}"
            placeholder="{{ 'templates.contact.form.name' | t }}"
          >
          <label class="field__label" for="ContactForm-name">{{ 'templates.contact.form.name' | t }}</label>
        </div>
        <div class="field field--with-error">
          <input
            autocomplete="email"
            type="email"
            id="ContactForm-email"
            class="field__input"
            name="contact[email]"
            spellcheck="false"
            autocapitalize="off"
            value="{% if form.email %}{{ form.email }}{% elsif customer %}{{ customer.email }}{% endif %}"
            aria-required="true"
            {% if form.errors contains 'email' %}
              aria-invalid="true"
              aria-describedby="ContactForm-email-error"
            {% endif %}
            placeholder="{{ 'templates.contact.form.email' | t }}"
          >
          <label class="field__label" for="ContactForm-email">
            {{- 'templates.contact.form.email' | t }}
            <span aria-hidden="true">*</span></label
          >
          {%- if form.errors contains 'email' -%}
            <small class="contact__field-error" id="ContactForm-email-error">
              <span class="visually-hidden">{{ 'accessibility.error' | t }}</span>
              <span class="form__message">
                <span class="svg-wrapper">
                  {{- 'icon-error.svg' | inline_asset_content -}}
                </span>
                {{- form.errors.translated_fields.email | capitalize }}
                {{ form.errors.messages.email -}}
              </span>
            </small>
          {%- endif -%}
        </div>
      </div>
      <div class="field">
        <input
          type="tel"
          id="ContactForm-phone"
          class="field__input"
          autocomplete="tel"
          name="contact[{{ 'templates.contact.form.phone' | t }}]"
          pattern="[0-9\-]*"
          value="{% if form.phone %}{{ form.phone }}{% elsif customer %}{{ customer.phone }}{% endif %}"
          placeholder="{{ 'templates.contact.form.phone' | t }}"
        >
        <label class="field__label" for="ContactForm-phone">{{ 'templates.contact.form.phone' | t }}</label>
      </div>
      <div class="field">
        <textarea
          rows="10"
          id="ContactForm-body"
          class="text-area field__input"
          name="contact[{{ 'templates.contact.form.comment' | t }}]"
          placeholder="{{ 'templates.contact.form.comment' | t }}"
        >
          {{- form.body -}}
        </textarea>
        <label class="form__label field__label" for="ContactForm-body">
          {{- 'templates.contact.form.comment' | t -}}
        </label>
      </div>
      <div class="contact__button">
        <button type="submit" class="button">
          {{ 'templates.contact.form.send' | t }}
        </button>
      </div>
    {%- endform -%}
  </div>
</div>

{% schema %}
{
  "name": "t:sections.contact-form.name",
  "tag": "section",
  "class": "section",
  "disabled_on": {
    "groups": ["header", "footer"]
  },
  "settings": [
    {
      "type": "inline_richtext",
      "id": "heading",
      "default": "t:sections.contact-form.settings.title.default",
      "label": "t:sections.contact-form.settings.title.label"
    },
    {
      "type": "select",
      "id": "heading_size",
      "options": [
        {
          "value": "h2",
          "label": "t:sections.all.heading_size.options__1.label"
        },
        {
          "value": "h1",
          "label": "t:sections.all.heading_size.options__2.label"
        },
        {
          "value": "h0",
          "label": "t:sections.all.heading_size.options__3.label"
        },
        {
          "value": "hxl",
          "label": "t:sections.all.heading_size.options__4.label"
        },
        {
          "value": "hxxl",
          "label": "t:sections.all.heading_size.options__5.label"
        }
      ],
      "default": "h1",
      "label": "t:sections.all.heading_size.label"
    },
    {
      "type": "color_scheme",
      "id": "color_scheme",
      "label": "t:sections.all.colors.label",
      "default": "scheme-1"
    },
    {
      "type": "header",
      "content": "t:sections.all.padding.section_padding_heading"
    },
    {
      "type": "range",
      "id": "padding_top",
      "min": 0,
      "max": 100,
      "step": 4,
      "unit": "px",
      "label": "t:sections.all.padding.padding_top",
      "default": 36
    },
    {
      "type": "range",
      "id": "padding_bottom",
      "min": 0,
      "max": 100,
      "step": 4,
      "unit": "px",
      "label": "t:sections.all.padding.padding_bottom",
      "default": 36
    }
  ],
  "presets": [
    {
      "name": "t:sections.contact-form.presets.name"
    }
  ]
}
{% endschema %}
Replies 4 (4)

mageplaza-cs
Shopify Partner
389 30 65

Hi @hjth96 ,

I am from Mageplaza - Shopify solution expert.

 

You can try the code below that I have customized from your original code above.

{{ 'section-contact-form.css' | asset_url | stylesheet_tag }}

{%- style -%}
    .section-{{ section.id }}-padding {
    padding-top: {{ section.settings.padding_top | times: 0.75 | round: 0 }}px;
    padding-bottom: {{ section.settings.padding_bottom | times: 0.75 | round: 0 }}px;
    }

    @media screen and (min-width: 750px) {
    .section-{{ section.id }}-padding {
    padding-top: {{ section.settings.padding_top }}px;
    padding-bottom: {{ section.settings.padding_bottom }}px;
    }
    }

    .hidden {
    display: none;
    }
{%- endstyle -%}

<div class="color-{{ section.settings.color_scheme }} gradient">
    <div class="contact page-width page-width--narrow section-{{ section.id }}-padding">
        {%- if section.settings.heading != blank -%}
            <h2 class="title title-wrapper--no-top-margin inline-richtext {{ section.settings.heading_size }}{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--slide-in{% endif %}">
                {{ section.settings.heading }}
            </h2>
        {%- else -%}
        {%- endif -%}
        {%- liquid
            assign contact_form_class = 'isolate'
            if settings.animations_reveal_on_scroll
                assign contact_form_class = 'isolate scroll-trigger animate--slide-in'
            endif
            <h2 class="visually-hidden">{{ 'templates.contact.form.title' | t }}</h2>
        -%}

        {%- form 'contact', id: 'ContactForm', class: contact_form_class -%}
        {%- if form.posted_successfully? -%}
            <h2 class="form-status form-status-list form__message" tabindex="-1" autofocus>
                {{- 'icon-success.svg' | inline_asset_content -}}
                {{ 'templates.contact.form.post_success' | t }}
            </h2>
            <div style="text-align: center;">
                <img src="https://cdn.shopify.com/s/files/1/0896/4807/1021/files/Notes.png?v=1741802045" alt="Form submitted successfully" style="width: 300px; height: auto;" class="success-image">
            </div>
            <div style="text-align: center; margin-top: 20px;">
                <button onclick="window.location.href='/contact'" class="button">
                    {{ 'templates.contact.form.another_question' | t }}
                </button>
            </div>
            <script>
                document.getElementById('ContactForm').classList.add('hidden');
            </script>
        {%- elsif form.errors -%}
            <div class="form__message">
                <h2 class="form-status caption-large text-body" role="alert" tabindex="-1" autofocus>
                    {{- 'icon-error.svg' | inline_asset_content -}}
                    {{ 'templates.contact.form.error_heading' | t }}
                </h2>
            </div>
            <ul class="form-status-list caption-large" role="list">
                <li>
                    <a href="#ContactForm-email" class="link">
                        {{ form.errors.translated_fields.email | capitalize }}
                        {{ form.errors.messages.email }}
                    </a>
                </li>
            </ul>
        {%- endif -%}
            <div class="contact__fields">
                <div class="field">
                    <input
                            class="field__input"
                            autocomplete="name"
                            type="text"
                            id="ContactForm-name"
                            name="contact[{{ 'templates.contact.form.name' | t }}]"
                            value="{% if form.name %}{{ form.name }}{% elsif customer %}{{ customer.name }}{% endif %}"
                            placeholder="{{ 'templates.contact.form.name' | t }}"
                    >
                    <label class="field__label" for="ContactForm-name">{{ 'templates.contact.form.name' | t }}</label>
                </div>
                <div class="field field--with-error">
                    <input
                            autocomplete="email"
                            type="email"
                            id="ContactForm-email"
                            class="field__input"
                            name="contact[email]"
                            spellcheck="false"
                            autocapitalize="off"
                            value="{% if form.email %}{{ form.email }}{% elsif customer %}{{ customer.email }}{% endif %}"
                            aria-required="true"
                            {% if form.errors contains 'email' %}
                            {% endif %}
                            aria-invalid="true"
                            aria-describedby="ContactForm-email-error"
                            placeholder="{{ 'templates.contact.form.email' | t }}"
                    >
                    <label class="field__label" for="ContactForm-email">
                        {{- 'templates.contact.form.email' | t }}
                        <span aria-hidden="true">*</span></label
                    >
                    {%- if form.errors contains 'email' -%}
                        <small class="contact__field-error" id="ContactForm-email-error">
                            <span class="visually-hidden">{{ 'accessibility.error' | t }}</span>
                            <span class="form__message">
                <span class="svg-wrapper">
                  {{- 'icon-error.svg' | inline_asset_content -}}
                </span>
                {{- form.errors.translated_fields.email | capitalize }}
                                {{ form.errors.messages.email -}}
              </span>
                        </small>
                    {%- endif -%}
                </div>
            </div>
            <div class="field">
                <input
                        type="tel"
                        id="ContactForm-phone"
                        class="field__input"
                        autocomplete="tel"
                        name="contact[{{ 'templates.contact.form.phone' | t }}]"
                        pattern="[0-9\-]*"
                        value="{% if form.phone %}{{ form.phone }}{% elsif customer %}{{ customer.phone }}{% endif %}"
                        placeholder="{{ 'templates.contact.form.phone' | t }}"
                >
                <label class="field__label" for="ContactForm-phone">{{ 'templates.contact.form.phone' | t }}</label>
            </div>
            <div class="field">
        <textarea
                rows="10"
                id="ContactForm-body"
                class="text-area field__input"
                name="contact[{{ 'templates.contact.form.comment' | t }}]"
                placeholder="{{ 'templates.contact.form.comment' | t }}"
        >
          {{- form.body -}}
        </textarea>
                <label class="form__label field__label" for="ContactForm-body">
                    {{- 'templates.contact.form.comment' | t -}}
                </label>
            </div>
            <div class="contact__button">
                <button type="submit" class="button">
                    {{ 'templates.contact.form.send' | t }}
                </button>
            </div>
        {%- endform -%}
    </div>
</div>

{% schema %}
{
  "name": "t:sections.contact-form.name",
  "tag": "section",
  "class": "section",
  "disabled_on": {
    "groups": ["header", "footer"]
  },
  "settings": [
    {
      "type": "inline_richtext",
      "id": "heading",
      "default": "t:sections.contact-form.settings.title.default",
      "label": "t:sections.contact-form.settings.title.label"
    },
    {
      "type": "select",
      "id": "heading_size",
      "options": [
        {
          "value": "h2",
          "label": "t:sections.all.heading_size.options__1.label"
        },
        {
          "value": "h1",
          "label": "t:sections.all.heading_size.options__2.label"
        },
        {
          "value": "h0",
          "label": "t:sections.all.heading_size.options__3.label"
        },
        {
          "value": "hxl",
          "label": "t:sections.all.heading_size.options__4.label"
        },
        {
          "value": "hxxl",
          "label": "t:sections.all.heading_size.options__5.label"
        }
      ],
      "default": "h1",
      "label": "t:sections.all.heading_size.label"
    },
    {
      "type": "color_scheme",
      "id": "color_scheme",
      "label": "t:sections.all.colors.label",
      "default": "scheme-1"
    },
    {
      "type": "header",
      "content": "t:sections.all.padding.section_padding_heading"
    },
    {
      "type": "range",
      "id": "padding_top",
      "min": 0,
      "max": 100,
      "step": 4,
      "unit": "px",
      "label": "t:sections.all.padding.padding_top",
      "default": 36
    },
    {
      "type": "range",
      "id": "padding_bottom",
      "min": 0,
      "max": 100,
      "step": 4,
      "unit": "px",
      "label": "t:sections.all.padding.padding_bottom",
      "default": 36
    }
  ],
  "presets": [
    {
      "name": "t:sections.contact-form.presets.name"
    }
  ]
}
{% endschema %}

 

If it doesn't work as expected, please send me the URL of your website so I can accurately identify the contact form element.

 

Please let me know if it works as expected!

Best regards

 

Mageplaza | Top-Rated Shopify Agency | Trusted by 230,000+ worldwide merchants


If our suggestion works for you, please give it a Like or mark it as a Solution!


Should you have any questions or concerns, feel free to contact us via consultant@mageplaza.com

hjth96
Excursionist
17 1 1

I got this error when I copied and pasted it in the contact-form.liquid code: 

  • Liquid syntax error (line 34): Unknown tag ' <h2 class="visually-hidden">{{ 'templates.contact.form.title' | t }}</h2>'

My store url is ninelivesdesigns.co and password is Jiji

mageplaza-cs
Shopify Partner
389 30 65

@hjth96 

 

Please replace it with the new code below:

{{ 'section-contact-form.css' | asset_url | stylesheet_tag }}

{%- style -%}
    .section-{{ section.id }}-padding {
    padding-top: {{ section.settings.padding_top | times: 0.75 | round: 0 }}px;
    padding-bottom: {{ section.settings.padding_bottom | times: 0.75 | round: 0 }}px;
    }

    @media screen and (min-width: 750px) {
    .section-{{ section.id }}-padding {
    padding-top: {{ section.settings.padding_top }}px;
    padding-bottom: {{ section.settings.padding_bottom }}px;
    }
    }
{%- endstyle -%}

<div class="color-{{ section.settings.color_scheme }} gradient">
    <div class="contact page-width page-width--narrow section-{{ section.id }}-padding">
        {%- if section.settings.heading != blank -%}
            <h2 class="title title-wrapper--no-top-margin inline-richtext {{ section.settings.heading_size }}{% if settings.animations_reveal_on_scroll %} scroll-trigger animate--slide-in{% endif %}">
                {{ section.settings.heading }}
            </h2>
        {%- else -%}
            <h2 class="visually-hidden">{{ 'templates.contact.form.title' | t }}</h2>
        {%- endif -%}
        {%- liquid
            assign contact_form_class = 'isolate'
            if settings.animations_reveal_on_scroll
                assign contact_form_class = 'isolate scroll-trigger animate--slide-in'
            endif
        -%}

        {%- form 'contact', id: 'ContactForm', class: contact_form_class -%}
            <div class="contact__fields">
                <div class="field">
                    <input
                            class="field__input"
                            autocomplete="name"
                            type="text"
                            id="ContactForm-name"
                            name="contact[{{ 'templates.contact.form.name' | t }}]"
                            value="{% if form.name %}{{ form.name }}{% elsif customer %}{{ customer.name }}{% endif %}"
                            placeholder="{{ 'templates.contact.form.name' | t }}"
                    >
                    <label class="field__label" for="ContactForm-name">{{ 'templates.contact.form.name' | t }}</label>
                </div>
                <div class="field field--with-error">
                    <input
                            autocomplete="email"
                            type="email"
                            id="ContactForm-email"
                            class="field__input"
                            name="contact[email]"
                            spellcheck="false"
                            autocapitalize="off"
                            value="{% if form.email %}{{ form.email }}{% elsif customer %}{{ customer.email }}{% endif %}"
                            aria-required="true"
                            {% if form.errors contains 'email' %}
                                aria-invalid="true"
                                aria-describedby="ContactForm-email-error"
                            {% endif %}
                            placeholder="{{ 'templates.contact.form.email' | t }}"
                    >
                    <label class="field__label" for="ContactForm-email">
                        {{- 'templates.contact.form.email' | t }}
                        <span aria-hidden="true">*</span></label
                    >
                    {%- if form.errors contains 'email' -%}
                        <small class="contact__field-error" id="ContactForm-email-error">
                            <span class="visually-hidden">{{ 'accessibility.error' | t }}</span>
                            <span class="form__message">
                <span class="svg-wrapper">
                  {{- 'icon-error.svg' | inline_asset_content -}}
                </span>
                {{- form.errors.translated_fields.email | capitalize }}
                                {{ form.errors.messages.email -}}
              </span>
                        </small>
                    {%- endif -%}
                </div>
            </div>
            <div class="field">
                <input
                        type="tel"
                        id="ContactForm-phone"
                        class="field__input"
                        autocomplete="tel"
                        name="contact[{{ 'templates.contact.form.phone' | t }}]"
                        pattern="[0-9\-]*"
                        value="{% if form.phone %}{{ form.phone }}{% elsif customer %}{{ customer.phone }}{% endif %}"
                        placeholder="{{ 'templates.contact.form.phone' | t }}"
                >
                <label class="field__label" for="ContactForm-phone">{{ 'templates.contact.form.phone' | t }}</label>
            </div>
            <div class="field">
        <textarea
                rows="10"
                id="ContactForm-body"
                class="text-area field__input"
                name="contact[{{ 'templates.contact.form.comment' | t }}]"
                placeholder="{{ 'templates.contact.form.comment' | t }}"
        >
          {{- form.body -}}
        </textarea>
                <label class="form__label field__label" for="ContactForm-body">
                    {{- 'templates.contact.form.comment' | t -}}
                </label>
            </div>
            <div class="contact__button">
                <button type="submit" class="button">
                    {{ 'templates.contact.form.send' | t }}
                </button>
            </div>
        {%- endform -%}
        {%- if form.posted_successfully? -%}
            <h2 class="form-status form-status-list form__message" tabindex="-1" autofocus>
                {{- 'icon-success.svg' | inline_asset_content -}}
                {{ 'templates.contact.form.post_success' | t }}
            </h2>
            <div style="text-align: center;">
                <img src="https://cdn.shopify.com/s/files/1/0896/4807/1021/files/Notes.png?v=1741802045"
                     alt="Form submitted successfully" style="width: 300px; height: auto;" class="success-image">
            </div>
            <div style="text-align: center; margin-top: 20px;">
                <button onclick="window.location.href='/contact'" class="button">
                    {{ 'templates.contact.form.another_question' | t }}
                </button>
            </div>
            <script>
                document.getElementById('ContactForm').classList.add('hidden');
            </script>
        {%- elsif form.errors -%}
            <div class="form__message">
                <h2 class="form-status caption-large text-body" role="alert" tabindex="-1" autofocus>
                    {{- 'icon-error.svg' | inline_asset_content -}}
                    {{ 'templates.contact.form.error_heading' | t }}
                </h2>
            </div>
            <ul class="form-status-list caption-large" role="list">
                <li>
                    <a href="#ContactForm-email" class="link">
                        {{ form.errors.translated_fields.email | capitalize }}
                        {{ form.errors.messages.email }}
                    </a>
                </li>
            </ul>
        {%- endif -%}
    </div>
</div>

{% schema %}
{
  "name": "t:sections.contact-form.name",
  "tag": "section",
  "class": "section",
  "disabled_on": {
    "groups": [
      "header",
      "footer"
    ]
  },
  "settings": [
    {
      "type": "inline_richtext",
      "id": "heading",
      "default": "t:sections.contact-form.settings.title.default",
      "label": "t:sections.contact-form.settings.title.label"
    },
    {
      "type": "select",
      "id": "heading_size",
      "options": [
        {
          "value": "h2",
          "label": "t:sections.all.heading_size.options__1.label"
        },
        {
          "value": "h1",
          "label": "t:sections.all.heading_size.options__2.label"
        },
        {
          "value": "h0",
          "label": "t:sections.all.heading_size.options__3.label"
        },
        {
          "value": "hxl",
          "label": "t:sections.all.heading_size.options__4.label"
        },
        {
          "value": "hxxl",
          "label": "t:sections.all.heading_size.options__5.label"
        }
      ],
      "default": "h1",
      "label": "t:sections.all.heading_size.label"
    },
    {
      "type": "color_scheme",
      "id": "color_scheme",
      "label": "t:sections.all.colors.label",
      "default": "scheme-1"
    },
    {
      "type": "header",
      "content": "t:sections.all.padding.section_padding_heading"
    },
    {
      "type": "range",
      "id": "padding_top",
      "min": 0,
      "max": 100,
      "step": 4,
      "unit": "px",
      "label": "t:sections.all.padding.padding_top",
      "default": 36
    },
    {
      "type": "range",
      "id": "padding_bottom",
      "min": 0,
      "max": 100,
      "step": 4,
      "unit": "px",
      "label": "t:sections.all.padding.padding_bottom",
      "default": 36
    }
  ],
  "presets": [
    {
      "name": "t:sections.contact-form.presets.name"
    }
  ]
}
{% endschema %}

 

Mageplaza | Top-Rated Shopify Agency | Trusted by 230,000+ worldwide merchants


If our suggestion works for you, please give it a Like or mark it as a Solution!


Should you have any questions or concerns, feel free to contact us via consultant@mageplaza.com

hjth96
Excursionist
17 1 1

Hey, that did not work unfortunately. Now when I submit the form, the success message and image do not show. The form, however, still shows.