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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line