Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

One form on multiple pages [not sending]

Started by Muut Archive 10 years ago · 8 replies · 848 views
10 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!

10 years ago

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

10 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 %}
---
10 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
---
10 years ago

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

10 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 2566 9 years ago
Archive · by Muut Archive, 9 years ago
2 1690 9 years ago
Archive · by Muut Archive, 9 years ago
2 4982 9 years ago
Archive · by Muut Archive, 9 years ago
1 3806 9 years ago
Archive · by Muut Archive, 9 years ago
3 1799 9 years ago