Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

One form on multiple pages [not sending]

Started by Muut Archive 9 years ago · 8 replies · 729 views
9 years ago

The structure:

homepage.md has 2 forms as

YAML
forms:
  contact-form:
      fields:
  subscribe-form:
      fields:

when you enter page B.md in b.html.twig it has

TWIG
{% include 'partials/contact_form.html.twig' with { formName : 'contact-form' } %}

the "partials/contact_form.html.twig" has

TWIG
{% include "forms/form.html.twig" with { form: forms(formName) } %}

So the form is nicely found in home.md (somehow) and the form is rendered as expected.

Problem: when i try to submit something, he page reloads and nothing is sent, file data.txt is not created as well.

Any help is very appreciated!

9 years ago

I use Ajax submit to do one form on multiple page, here.

9 years ago

Is there any way to use CMS built in functionality for that without Ajax?

9 years ago

I try builtin way without success, only Ajax worked for me. Here's what I do:

TWIG

...
{# Include the form in page template where all the page should have the same form #}
{% set formdata = page.find('/form').header.form %}
{% include "forms/form.html.twig" with {'form': formdata} %}
...
...
...
{% block javascript %}
...
<script>
// submit the form via Ajax
$(document).ready(function(){
    $('#oneform').submit(function(e) {
        e.preventDefault();
        $.ajax({
            url: '{{ page.find('/form').url }}',
            type: form.attr('method'),
            dataType: 'html',
            data: form.serialize(),
            success: function(result) { 
                $('#form-result').html(result);
            }
        });
    });
});
</script>
...
{% endblock %}
---
9 years ago

I think i am close, and the form is working on the localhost, but on a remote i am getting this error:

PHP

[2017-04-06 14:13:08] grav.CRITICAL: Argument 1 passed to Grav\Plugin\EmailPlugin::buildMessage() must be of the type array, null given
---
9 years ago

OK, some testing, and looks like i did find i solution

9 years ago

The result:

  1. All the forms: are placed on one page, they are nicely found by grav
  2. Then you can include {% include "forms/form.html.twig" with { formName : 'contact-form' }%} inside your template and everything should be working

If something is not working, check the validity of the frontmatter, my case was wrong tabulation. Simple as that.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1327 9 years ago
Archive · by Muut Archive, 9 years ago
2 921 9 years ago
Archive · by Muut Archive, 9 years ago
2 4050 9 years ago
Archive · by Muut Archive, 9 years ago
1 2928 9 years ago
Archive · by Muut Archive, 9 years ago
3 1107 9 years ago