0

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

  • check this http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Abhishek Sharma Nov 20 '15 at 05:15
  • You're not checking if `$_SESSION['login_user']` is set already. You are passing that value direct to your SQL. You aren't checking if `$_POST["room_id"]` is set and are also passing that direct to your query. SQL injection deploy.. – chris85 Nov 20 '15 at 05:17

2 Answers2

2

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.

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
  • no error... i saw the log actually have run the join.php and id is successfully passed to it.. is that important to mention the status code of join.php is 302 found? – novice_matthew Nov 20 '15 at 12:00
1

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

Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20