Javascript 如何使用ajax和普通帖子同时提交表单?

Javascript 如何使用ajax和普通帖子同时提交表单?,javascript,php,jquery,ajax,forms,Javascript,Php,Jquery,Ajax,Forms,我已经创建了一个表单,它可以将数据提交到外部URL,同时我还想将数据保存到我的数据库中。我尝试了ajaxsubmit,并尝试在ajax成功中使用POST 这是我的代码: <form action="http://www.blabla/post" method="post" id="myfrom"> ..... .... </form> <script> $('#myfrom').submit(function() { e.preventDefault

我已经创建了一个表单,它可以将数据提交到外部URL,同时我还想将数据保存到我的数据库中。我尝试了
ajax
submit,并尝试在ajax成功中使用
POST

这是我的代码:

<form action="http://www.blabla/post" method="post" id="myfrom">
.....
....
</form>

<script>

 $('#myfrom').submit(function() {
  e.preventDefault();
     $.ajax({
              url: 'admin-ajax.php', 
              type: 'POST', 
              dataType: 'html', 
              data: $('#myfrom').serialize(), 
              success: function() {
                   $('#myfrom').submit();
              },
              error: function(e) {
                  console.log(e);
              }
          }); 
  });
</script> 

.....
....
$('#myfrom')。提交(函数(){
e、 预防默认值();
$.ajax({
url:'admin ajax.php',
键入:“POST”,
数据类型:“html”,
数据:$('#myfrom')。序列化(),
成功:函数(){
$('#myfrom')。提交();
},
错误:函数(e){
控制台日志(e);
}
}); 
});

但这里Ajax不起作用,表单将仅提交正常帖子

在ajax调用后返回false以停止正常表单提交,如下所示:

$('#myfrom').submit(function() {                     
      $.ajax({
              url: 'admin-ajax.php', 
              type: 'POST', 
              dataType: 'html', 
              data: $('#myfrom').serialize(), 
              success: function() {
                    $('#myfrom').submit();
              },
              error: function(e) {
                    console.log(e);
              }
          });
     return false; 
});
更改此项:

$('#myfrom').submit(function() {
为此:

$('#myfrom').submit(function(e) {

您尚未在参数中传递事件,因此表单将被提交


我认为这是另一种方式:

$('yourButton').on('click', function(e){
    e.preventDefault(); // stops the buttons behavior
    $.ajax({
         url: 'admin-ajax.php', 
         type: 'POST', 
         dataType: 'html', 
         data: $('#myfrom').serialize(), 
         success: function() {
             $('#myfrom').submit(); // submits the form if success
         },
         error: function(e) {
             console.log(e);
         }
    });
});

$('#postData')。单击(函数(e){
e、 预防默认值();
$.ajax({
url:'admin ajax.php',
键入:“POST”,
数据类型:“html”,
数据:$('#myfrom')。序列化(),
成功:函数(){
$('#myfrom')。提交();
},
错误:函数(e){
控制台日志(e);
}
}); 
});

你试试这个。用一个按钮代替提交按钮。

您在console.log中得到了什么?您是否打印了php页面中的数据?正常的帖子是一个全新的页面请求。在进行ajax调用之前,必须防止执行submit。请尝试返回false..而不是e.preventDefault();并确保没有任何错误输入
return false
,而不是
e.preventDefault()但是现在ajax请求以无限循环的方式进行,我在回答中也添加了这个,但是现在会not@EhsanSajjad哦,我明白了,我想我只是及时记下了。:)是的,这也是我添加它的主要原因,因为我通常会返回false来停止表单提交和停止事件的默认行为。:@EhsanSajjad我想你知道
返回false
同时做两件事,即prevent default()和&e.stopPropagation()
,那么它是好是坏呢??
<script>
   $('#postData').click(function(e) {
       e.preventDefault();
       $.ajax({
              url: 'admin-ajax.php', 
              type: 'POST', 
              dataType: 'html', 
              data: $('#myfrom').serialize(), 
              success: function() {
                   $('#myfrom').submit();
              },
              error: function(e) {
                  console.log(e);
              }
        }); 
    });
</script> 

<form action="http://www.blabla/post" method="post" id="myfrom">
<input type="button" value="post" id="postData">
</form>