从Javascript调用AJAX后,标头PHP无法正常工作

so, this is probably a dumb question, but is it possible to execute the header function in a php file if I'm getting a response with AJAX?

In my case, I have a login form that gets error codes from the PHP script (custom error numbers hardcoded by me for testing) through AJAX (to avoid reloading the page) and alerts the associated message with JS, but if the username and password is correct, I want to create a PHP cookie and do a redirect. However I think AJAX only allows getting data, right?

This is my code:

JS

$.ajax({
    type: 'POST',
    url: 'validate.php',
    data: $this.serialize(),
    success: function(response) {
        var responseCode = parseInt(response);
        alert(codes[responseCode]);
    }
});

PHP

if(empty($user)){
    echo 901;
}else{
    if(hash_equals($user->hash, crypt($password, $user->hash))){
        setCookie(etc...); //this is
        header('admin.php'); //what is not executing because I'm using AJAX
    }else{
        echo 902;
    }
}

Please sorry if the question doesn't even make sense at all but I couldn't find a solution. Thanks in advance!

EDIT: I did not include the rest of the code to avoid complicating stuff, but if you need it for giving an anwser I'll add it right away! (:

You're right, you can't intermix like that. The php would simply execute right away, since it has no knowledge of the javascript and will be interpreted by the server at runtime, whereas the js will be interpreted by the browser.

One possible solution is to set a cookie with js and redirect with js as well. Or you could have the server that receives the login request set the cookie when the login request succeeds and have the js do the redirect after it gets a successful response from the server.

You can't do like that because ajax request process in backed and return the particular response and if you want to store the cookies and redirect then you should do it in javascript side while you get the response success

$.ajax({
    type: 'POST',
    url: 'validate.php',
    data: $this.serialize(),
    success: function(response) {
        var responseCode = parseInt(response);
        alert(codes[responseCode]);

        window.location = "admin.php";
    }
});

if(empty($user)){
    setCookie(etc...); //this is
    echo 901;
}else{
    if(hash_equals($user->hash, crypt($password, $user->hash))){
        echo response// what every you want to store
    }else{
        echo 902;
    }
}

If the ajax response satisfies your condition for redirection, you can use below:

$.ajax({
    type: 'POST',
    url: 'validate.php',
    data: $this.serialize(),
    success: function(response) {
        var responseCode = parseInt(response);
        alert(codes[responseCode]);
        window.location="%LINK HERE%";
    }
});

It's kind of ironic that you use ajax to avoid loading the page, but you'll be redirecting in another page anyway.

test sending data in json format:

Javascript

$.ajax({
   type: 'POST',
   url: 'validate.php',
   data: $this.serialize(),
   success: function(response) {
      if(response.success){
         window.location="%LINK HERE%";
      }else{
         var responseCode = parseInt(response.code);
         alert(responseCode);
         ...
      }
   }
});

PHP

header("Content-type: application/json");

if(empty($user)){
    echo json_encode(['success' => false, 'code' => 901]);
}else{
    if(hash_equals($user->hash, crypt($password, $user->hash))){
       echo json_encode(['success' => true, 'data' => response]);
    }else{
       echo json_encode(['success' => false, 'code' => 902]);
    }
}