phpsession-variablesmysql-insert-id

Passing PHP Session Variable to 'Include' Page Issue


I'm working with a main PHP page that has four included templates:

<?php session_start(); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<title>Example Page</title>
</head>
<body>
<?php include ("script/select.class.php"); ?>
<div class="content">
<?php 
    include("./templates/one.php");
    include("./templates/two.php");
    include("./templates/three.php");
    include("./templates/four.php");
?>
</div>
</body>
</html>

'two.php' contains a form (the_form) submitted via AJAX:

$('#button').click(function(){
        $.ajax({
            type: "POST",
            url: "script/add_new.php",
            data: $("#the_form").serialize(),
            success: function(data){
                newID = data;
                alert(newID); 
            }
        });
});

On 'add_new.php', once the INSERT is completed, I have this PHP code:

if (!mysqli_query($con,$sql)) {
    die('Error: ' . mysqli_error($con));
}

$_SESSION['new_id'] = mysqli_insert_id($con);

echo $_SESSION['new_id'];

The problem I'm experiencing is that even though $_SESSION['new_id'] will pass back through the Ajax 'data' var and show up in the 'alert', I get an Unidentified Index error if I attempt to display on the next tempate page (three.php) using <?php echo $_SESSION['new_id'] ?>.

Any suggestions?


Solution

  • Cheery is correct. I'll need to take a different approach.