php标题无法正常工作,可能出现什么错误?

In lobby.php

<script>
function join_game(roomid){
    $.ajax({
        method: "post",
        dataType: "json",
        url: "join.php",
        data: {room_id: roomid}

    });
}
<script>

In join.php

<?php
session_start();
include("connect.php");
global $conn;
$username = $_SESSION['login_user'];
$id = $_POST["room_id"];
$sql = "UPDATE  check_exist
    SET     player_join = '$username' , game_exist_lobby = '0'
    WHERE   ceid = '$id'";
mysqli_query($conn, $sql);
header("location: game.php?last_id=" . $id);
mysqli_close($conn);

I don't understand why the header didn't work and remain at the page, but the SQL statements is working towards the database and updates the data, and I don't know how to debug, similar code is working in other php, can anyone tell me any possible error to make this problem and any way to debug? Thanks

Redirect will not work with ajax.

try like this,

In ajax , add success.

success: function(data){
    window.open("game.php?last_id="+data);
}

In php,

echo $id;

$id will come in as data in success, use that and redirect.

Final Code

$.ajax({
    method: "post",
    dataType: "json",
    url: "join.php",
    data: {room_id: roomid},
    success: function(data){
        window.open("game.php?last_id="+data);
       // use data from success or use room_id,
    }
});

In success,

use data from success or use room_id, if you are using room_id, no need to echo $id in php file.

Try ob_start(); and ob_flush(); put your obstart(); into your top of the page

use Javascript Use window.location.href

<?php
    echo "<script type='text/javascript'>window.location.href = 'page1.php';</script>"
    exit();
?>

OR Use the exit() method after the header redirect