admin管理员组

文章数量:1323150

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

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

Share edited Feb 6, 2014 at 20:14 Patrik Fröhler asked Feb 6, 2014 at 19:30 Patrik FröhlerPatrik Fröhler 1,2911 gold badge12 silver badges39 bronze badges 7
  • 1 So whats the problem? Does it work? What doesn't work? – Jake N Commented Feb 6, 2014 at 19:35
  • Try POST in all caps...not sure if that is madatory but is good practice – VIDesignz Commented Feb 6, 2014 at 19:35
  • Do you have JQuery installed?? – VIDesignz Commented Feb 6, 2014 at 19:36
  • Target the form to serialize $('#msg_form').serialize(); – VIDesignz 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. – Patrik Fröhler Commented Feb 6, 2014 at 19:40
 |  Show 2 more ments

1 Answer 1

Reset to default 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())

本文标签: Submit form without refreshing page ajaxphpJavaScriptStack Overflow