douchui1488 2014-02-06 19:30
浏览 115
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • duanmanpi9358 2014-02-07 00:45
    关注

    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())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?