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

453

I've form that submit data to external URL and the same time I want to save in my database. I've triedajax submit and try to sumbit normalPOST 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.

587

Answer

Solution:

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);
         }
    });
});
725

Answer

Solution:

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

874

Answer

Solution:

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; 
});

People are also looking for solutions to the problem: javascript - how to use jquery validate .js in wordpress for user name already exist?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.