1

  
$("form.formajax").submit(function(e) {

e.preventDefault();

var data = $(this).serialize();
var url = $(this).attr("action");
var form = $(this); // Add this line
$.post(url, data, function(data) {


$(form).children(".loginresult").html(data.loginresult);

if (data.success == 1) 
 {
  // SUBMIT the form here with POST Values (and redirect user)
  }



});
return false;
});
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


  <form class="login formajax" action="abc.php" method="post">
    <input type="email" >
    <input type= "password">
    <input type="submit">
    </form>

This is my code to submit a form using Ajax. It return some values.

It also return 1 or 0 with the name data.success

If this condition gets satisfied if (data.success == 1)

I want to submit the form (redirect the user + submit post value), but how?

Toby
  • 657
  • 1
  • 6
  • 18
  • Have you tried doing `$(this).submit()`? Or, are you looking for something else? – Nisarg Aug 27 '17 at 06:18
  • Not yet @NisargShah – Toby Aug 27 '17 at 06:20
  • [This](https://stackoverflow.com/a/11543243/5894241) answer shows how could could bind a callback to form submission completion event, and then you can redirect the user to another page. – Nisarg Aug 27 '17 at 06:22
  • Wait, i tried `$(.login).submit();` But it always submits the form, even when the condition is not satisfied. – Toby Aug 27 '17 at 06:25
  • I just posted the answer. Did you put the submit call inside the condition block? Did you see any errors in console? – Nisarg Aug 27 '17 at 06:30

2 Answers2

0

After successful form submission,you can redirect .

You can user jQuery#serialize.

Its will serialize the form data using key=value1 & key2-value2

$("form.formajax").submit(function(e) {

    e.preventDefault();

    var data = $(this).serialize();
    //somecode
    $.post(url, data, function(data) {

        if (data.success == 1) {
            var myNewUrl = "some_url ?" + data;
            //myNewUrl will be `url?key=value1 & key2-value2`
            $(location).attr('href', myNewUrl); //jQuery

        }
    });
    return false;
});

On redirected page you can retrieve the query string values

RIYAJ KHAN
  • 13,830
  • 5
  • 27
  • 48
0

You can submit the form using .submit() function on the form. So it could be done using either of these two statements:

$(this).submit(); // Since you are inside an event handler on the form.

Or,

$(".login").submit(); // Since login is the css class on the form.

Then, as explained here, you can listen to form submission completion event as:

$("form.formajax").bind('ajax:complete', function() {...});

$("form.formajax").submit(function(e) {

  e.preventDefault();

  var data = $(this).serialize();
  var url = $(this).attr("action");
  var form = $(this); // Add this line
  $.post(url, data, function(data) {


    $(form).children(".loginresult").html(data.loginresult);

    if (data.success == 1) {
      // SUBMIT the form here with POST Values (and redirect user)
      $(this).submit();
    }
  });
  return false;
});

// Wait for form submission.
$("form.formajax").bind('ajax:complete', function() {
  alert("Redirect the user here.");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<form class="login formajax" action="abc.php" method="post">
  <input type="email">
  <input type="password">
  <input type="submit">
</form>
Nisarg
  • 13,121
  • 5
  • 31
  • 48