admin管理员组文章数量:1323318
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
1 Answer
Reset to default 4Solved 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
版权声明:本文标题:Submit form without refreshing page ajax,php,javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742144571a2422731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论