最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to submit form using ajax and normal POST in same time? - Stack Overflow

programmeradmin8浏览0评论

I've form that submit data to external URL and the same time I want to save in my database. I've tried ajax submit and try to sumbit normal POST in ajax success.

Here my code:

<form action="/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> 

But here Ajax is not working form will submit normal post only.

I've form that submit data to external URL and the same time I want to save in my database. I've tried ajax submit and try to sumbit normal POST in ajax success.

Here my code:

<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> 

But here Ajax is not working form will submit normal post only.

Share Improve this question edited May 29, 2014 at 12:47 Jf Beaulac 5,2461 gold badge28 silver badges47 bronze badges asked May 29, 2014 at 12:13 MiurangaMiuranga 2,46311 gold badges53 silver badges81 bronze badges 4
  • what do you get in console.log? and did you print the data you get in your php page? – Indra Commented May 29, 2014 at 12:17
  • 2 a normal post is a whole new page request. You have to prevent submit from executing before making the ajax call. – Rob Schmuecker Commented May 29, 2014 at 12:17
  • try return false..instead of e.preventDefault(); and make sure you don't have any errors – harikrish Commented May 29, 2014 at 12:18
  • tried return falseinstead of e.preventDefault(); but now ajax request going in infinite loop – Miuranga Commented May 29, 2014 at 12:29
Add a ment  | 

3 Answers 3

Reset to default 5

change this:

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

to this:

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

You have not passed the event in the argument so form gets submitted.


This is another way as i see:

$('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);
         }
    });
});
<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>

you try this one. make a button in place of submit button.

return false after ajax call to stop the normal form submit like this:

$('#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; 
});
发布评论

评论列表(0)

  1. 暂无评论