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

A few questions about the form

Started by Muut Archive 9 years ago · 4 replies · 494 views
9 years ago

Is it possible to send the form without a redirect page? If Yes, how to do a dynamic submit button of the form, i.e. for example after pressing the word Submit was changed on the Sending and Sent.
For button may need to use ajax or api, but how and where to start?
Like, if you also care about these issues. )))
Thanks.

9 years ago

Ok, i had never actually tested this but thought it would work. It does require a fix to the form plugin which i'll commit/release soon:

  1. my page (form/ajax/form.md):

---html


title: Ajax Form
form:
name: ajax-form
action: /form/ajax.json

YAML
fields:
    name:
        label: Name
        size: small
        placeholder: 'Enter your name'
        type: text
        validate:
            required: true

buttons: 
    submit:
        type: submit
        value: Submit

process:
    message: 'Thank you from your Smallform Feedback!'

<div id="form-result"></div>

<script>
$(document).ready(function(){

JS
var form = $('#ajax-form');
form.submit(function(e) {
    console.log("in here");
    e.preventDefault();

    $.ajax({
        url: form.attr('action'),
        type: form.attr('method'),
        dataType: 'html',
        data: form.serialize(),
        success: function(result) {
            $('#form-result').html(result);
        }
    });
});

});
</script>

TXT
As the response is HTML an we just update the DIV with the message response, it needs a change to the `form.json.twig`:

{% extends 'partials/base.html.twig' %}

{% block content %}

TWIG
{{ content|raw }}
{% include "forms/form.html.twig" %}

{% endblock %}

TXT

9 years ago

Obviosly left my debug comment in there :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1333 9 years ago
Archive · by Muut Archive, 9 years ago
2 924 9 years ago
Archive · by Muut Archive, 9 years ago
2 4055 9 years ago
Archive · by Muut Archive, 9 years ago
1 2933 9 years ago
Archive · by Muut Archive, 9 years ago
3 1110 9 years ago