2

I want to submit a form without refreshing the page, from what I have read it should work with ajax, what am i doing wrong?

it all works with the php and stuff when I do this:

document.getElementById("msg_form").submit();

But I want it to submit without refreshing the page.

Part of Html:

<form name="msg_form_name" id="msg_form" class="email" action="mailer.php">
<p>Your E-mail:</p>
<input id="email_form" name="email" type="text" />
<p>Amount:</p>
<input id="amount_form" name="amount" class="amount_num" type="text" maxlength="5" />
<div id="msg_txt_lenght">characters left: 38</div>
<p>Message:</p>
<input id="message_form" name="message" class="message_form_lim" type="text" >
<input type="hidden" id="storeUrl_id" name="storeUrl_form" value="Nan"></form>
</form>

Part of javascript:

$('#msg_form').submit(function (e) {
    e.preventDefault();

$.ajax({             
type: 'post',
url: 'mailer.php',
data: $('form').serialize(),
success: function () {
 alert('form was submitted');
}
    });

    return false;
});

Thanks in advance.

EDIT: might have fixed it had it on submit but didn't send a submit

7
  • 1
    So whats the problem? Does it work? What doesn't work? Commented Feb 6, 2014 at 19:35
  • Try POST in all caps...not sure if that is madatory but is good practice Commented Feb 6, 2014 at 19:35
  • Do you have JQuery installed?? Commented Feb 6, 2014 at 19:36
  • Target the form to serialize $('#msg_form').serialize(); Commented Feb 6, 2014 at 19:38
  • I don't get the alert msg also and when It works and it post to the php I receive an email but, I don't get the email so for sure it don't work. yes I have JQuery installed. Commented Feb 6, 2014 at 19:40

1 Answer 1

4

Solved it just replaced:

$('#msg_form').submit(function (e) {
    e.preventDefault();

$.ajax({             
type: 'post',
url: 'mailer.php',
data: $('#msg_form').serialize(),
success: function () {
 alert('form was submitted');
}
    });

    return false;
});

with this instead

$.post('mailer.php', $('#msg_form').serialize())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.