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

Please help with ajax

Started by Muut Archive 9 years ago · 22 replies · 1262 views
9 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.

9 years ago

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

9 years ago

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

9 years ago

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

9 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.

9 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.

9 years ago

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

9 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>
---
9 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.

9 years ago

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

9 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>
9 years ago

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

9 years ago

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

9 years ago

I never noticed that. Testing now...

9 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.

9 years ago

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

9 years ago

Really no id... but it is defined!

YAML

        buttons:
            -
                type: submit
                id: button
                value: 'SEND'
                classes: 'button-form-home button1 w-button'
---
9 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 1330 9 years ago
Archive · by Muut Archive, 9 years ago
2 922 9 years ago
Archive · by Muut Archive, 9 years ago
2 4051 9 years ago
Archive · by Muut Archive, 9 years ago
1 2930 9 years ago
Archive · by Muut Archive, 9 years ago
3 1108 9 years ago