-1

I have a login form when user logs in he has to redirect with to participant-screen.php

Ajax script

    $.ajax({
      url: "controller.php",method: "POST",
      data: { loginData: $("#loginForm").serialize(),'action': 'login' },
      dataType: "text",
      success: function(response) {
        $("#showMessage").html(response);
        if (response == 'success') {// response coming as success but not redirecting
 // redirecting script
        }
      }
  });
Mr world wide
  • 4,696
  • 7
  • 43
  • 97

4 Answers4

4

You cannot use a header() redirect when you use ajax. Instead you have to redirect in the javascript part of your ajax when the request has been processed successfully. See for example How to redirect to another webpage in JavaScript/jQuery?.

Note that you would have to send information back from your php script (for example json) so that you can take the appropriate action in javascript.

Apart from that:

  • You have an sql injection problem, you should use a prepared statement instead of injecting variables directly into your query.
  • You should not use md5() for password hashing. See for example How can I store my users' passwords safely?
Community
  • 1
  • 1
jeroen
  • 91,079
  • 21
  • 114
  • 132
2

header() not supported in jquery you need to user windows.location.href

window.location.href="your url";
Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
1

You Can Try below method for Redirecting

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";
Snopzer
  • 1,602
  • 19
  • 31
-1

I got my solution

$.ajax({
url: "controller.php",
method: "POST",
data: { loginData : $("#loginForm").serialize(), 'action':'login'},
dataType: "json",
success: function (response) {
 if(response["success"]==true)
 {
  $("#showMessage").html(response['message']);
  location = '/learningcurve/participant.php?loginid='+response["id"];
  window.open(location);
 }else{
  $("#showMessage").html(response['message']);
 }
},
error: function (request, status, error) {
 $("#showMessage").html("OOPS! Something Went Wrong Please Try After Sometime!");
}
});
return false;

i used data type text replaced with json

Mr world wide
  • 4,696
  • 7
  • 43
  • 97