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

Please help with ajax

Started by Muut Archive 10 years ago · 22 replies · 1476 views
10 years ago

Use ajax to display the message on sending email from forms.

JS
<div id="form-result"></div>
<script>
$(document).ready(function(){

    var form = $('#contact-home');
    form.submit(function(e) {
        // prevent form submission
        e.preventDefault();

        // submit the form via Ajax
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            dataType: 'html',
            data: form.serialize(),
            success: function(result) {
                // Inject the result in the HTML
                $('#form-result').html(result);
            $('#button').text("Sent!");
            } 
        });
    });
});
</script>

"#form-result" is displayed, everything is fine.
But the button text in form is not changed after sending. Tell me how please.

10 years ago

you sure your button has an id of button? like : <button id="button"></button>

10 years ago

Of course all id are the same, but the value is not changed.

10 years ago

The button is not inside #form-result , right?

10 years ago

Does $("#button span").text("Sent!"); work? I found that in this SO answer.
A nice UX would be to disable the button, perhaps with $("#button").prop("disabled",true);
BTW I haven't tested the above.

10 years ago

@bleutzinn Interesting, I didn't know that jQuery rendered a span inside a button.
If it does not work, what happen if you run it with $('#button').text("Sent!"); first?
I think we would need to see your html too.

10 years ago

Nothing happens, tried 10-15 methods. Maybe the order of rendering data on the page?

10 years ago

The usual form

HTML

<form name="contact-home" action="/home" method="POST" id="contact-home" class="form-home w-clearfix">
...some fields
    <div class="buttons">
      <button class="button-form-home button1 w-button" type="submit">
            Send
       </button>
    </div>
     <input type="hidden" id="form-nonce" name="form-nonce" value="d84128be02b5bbb201116de2f7cf2635">
</form>
<div id="form-result"></div>
<script>
$(document).ready(function(){

    var form = $('#contact-home');
    form.submit(function(e) {
        // prevent form submission
        e.preventDefault();

        // submit the form via Ajax
        $.ajax({ 
            url: form.attr('action'),
            type: form.attr('method'),
            dataType: 'html',
            data: form.serialize(),
            success: function(result) {
                // Inject the result in the HTML
                $('#form-result').html(result);
            }
        });
    });
});
</script>
---
10 years ago

Once the form has successfully been submitted do you still want or need a button in the user interface? I suppose not, so you can simply add a text message in a (new) div that says "Sent !" or something like that.

10 years ago

I'm not seeing the id in the button element.

10 years ago

well, that was my first response! your button does not have an id.
Replace with

TXT
<button class="button-form-home button1 w-button" id="button" type="submit">
            Send
       </button>
10 years ago

Aside: @paulmassendari, how were you able to edit your answer once it appeared on the forum?

10 years ago

There is a pencil appearing next to your name just after you posted.

10 years ago

Block message sending works. But recently saw on the website a send button with a animation inside and a change of labels after posting. I think it is very modern and looks very cool.

10 years ago

@paulmassendari: Clever. I see it and thanks!
@Stanislav, cool if it works, yes :)

10 years ago

Really no id... but it is defined!

YAML

        buttons:
            -
                type: submit
                id: button
                value: 'SEND'
                classes: 'button-form-home button1 w-button'
---
10 years ago

I don't think you can set an id from the frontmatter for this field as id don't work everywhere.
try adding a unique class instead or call one of these existing class.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 2569 9 years ago
Archive · by Muut Archive, 9 years ago
2 1691 9 years ago
Archive · by Muut Archive, 9 years ago
2 4983 9 years ago
Archive · by Muut Archive, 9 years ago
1 3809 9 years ago
Archive · by Muut Archive, 9 years ago
3 1800 9 years ago