douchui1488
2014-02-06 19:30
浏览 110

提交表单而不刷新页面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

图片转代码服务由CSDN问答提供 功能建议

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

当我这样做时,这一切都与php和东西一起工作:

  document.getElementById(“msg_form”)  .submit(); 
   
 
 

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

Html的一部分:

 &lt; form name =“msg_form_name”id =“msg_form”class =“email”action =“mailer.php”&gt; 
&lt; p&gt;您的电子邮件:&lt;  ; / p&gt; 
&lt; input id =“email_form”name =“email”type =“text”/&gt; 
&lt; p&gt;金额:&lt; / p&gt; 
&lt; input id =“amount_form”name =“ 金额“class =”amount_num“type =”text“maxlength =”5“/&gt; 
&lt; div id =”msg_txt_lenght“&gt;字符左:38&lt; / div&gt; 
&lt; p&gt;消息:&lt; / p&gt  ; 
&lt; input id =“message_form”name =“message”class =“message_form_lim”type =“text”&gt; 
&lt; input type =“hidden”id =“storeUrl_id”name =“storeUrl_form”value =“Nan  “&GT;&LT; /形式&GT; 
&LT; /形式&GT; 
   
 
 

javascript的一部分:

  $('#msg_form')。submit(function(e){
 e。  preventDefault(); 
 
 $ .ajax({
type:'post',
url:'mailer.php',
data:$('form')。serialize(),
success:function(){  
提醒('表单已提交'); 
} 
}); 
 
返回false; 
}); 
   
 
 

谢谢 编辑:编辑:可能已修复它已提交,但没有发送提交

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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())
    
    点赞 打赏 评论

相关推荐 更多相似问题