提交表单而不刷新页面ajax,php,javascript?

时间:2022-11-24 15:19:06

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?

我想提交一个没有刷新页面的表单,从我读过它应该使用ajax,我做错了什么?

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

当我这样做时,它都适用于PHP和东西:

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

But I want it to submit without refreshing the page.

但我希望它在不刷新页面的情况下提交。

Part of Html:

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:

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

编辑:可能已修复它已提交但未发送提交

1 个解决方案

#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())

#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())