Home > other >  Why is my first session in PHP not sending data to my second?
Why is my first session in PHP not sending data to my second?

Time:11-18

i'm trying to have one session that inputs data from the page into the post array, and then stores it to the session array which then sends it to a session 2 where I want the information to be displayed. My first session is sending me to my session 2 page but the information is not being displayed. Can anyone tell me if there is something wrong with my code? Thanks

Session 1

<?php
session_start();

if(isset($_POST["employeeNameBox"]))
{
    $_SESSION["employeeName"] = $_POST["employeeNameBox"];
    header("Location: Session2.php");
}

if(isset($_POST["employeeidBox"]))
{
    $_SESSION["employeeid"] = $_POST["employeeidBox"];
    header("Location: Session2.php");
}

if(isset($_POST["phoneNumberBox"]))
{
    $_SESSION["phoneNumber"] = $_POST["phoneNumberBox"];
    header("Location: Session2.php");
}

if(isset($_POST["emailBox"]))
{
    $_SESSION["email"] = $_POST["emailBox"];
    header("Location: Session2.php");
}

if(isset($_POST["positionBox"]))
{
    $_SESSION["position"] = $_POST["positionBox"];
    header("Location: Session2.php");
}

if(isset($_POST["ITProjectBox"]))
{
    $_SESSION["ITProject"] = $_POST["ITProjectBox"];
    header("Location: Session2.php");
}

?>

<html>

<body>

<?php include('Header.php'); ?>
<?php include('Menu.php'); ?>

<div class="content">

            <form method="POST">
        
            <input type="text" name="employeeNameBox" value="Employee name"> <br> 
            <input type="text" name="employeeidBox" value="Employee id"> <br>
            <input type="text" name="phoneNumberBox" value="Phone number"> <br> 
            <input type="text" name="emailBox" value="Email"> <br> 
            
            <input type="radio" id="Manager" name="positionBox" value="Manager"> 
            <label for="Manager">Manager</label><br>
            <input type="radio" id="Team Lead" name="positionBox" value="Team Lead"> 
            <label for="Team Lead">Team Lead</label><br> 
            <input type="radio" id="IT Developer" name="positionBox" value="IT Developer"> 
            <label for="IT Developer">IT Developer</label><br>
            <input type="radio" id="IT Analyst" name="positionBox" value="IT Analyst"> 
            <label for="IT Analyst">IT Analyst</label><br><br>


            <select name="ITProjectBox">
                <option value="Project A">Project A</option>
                <option value="Project B">Project B</option>
                <option value="Project C">Project C</option>
                <option value="Project D">Project D</option>
            </select> <br><br>

            <button type="submit">Submit Information</button>
            
            </form>

</div>


</div>

<?php include('Footer.php'); ?>


</body>
</html>

Session 2

<?php
session_start();

if(isset($_SESSION["employeeName"]))
{
    echo "<b>Employee name: </b>" .$_SESSION["employeeName"];
    echo("<br><br>");
}

if(isset($_SESSION["employeeid"]))
{
    echo("<b>Employee id: </b>" .$_SESSION["employeeid"]);
    echo("<br><br>");
}

if(isset($_SESSION["phoneNumber"]))
{
    echo("<b>Phone number: </b>" .$_SESSION["phoneNumber"]);
    echo("<br><br>");
}

if(isset($_SESSION["email"]))
{
    echo("<b>Email: </b>" . $_SESSION["email"]);
    echo("<br><br>");
}

if(isset($_SESSION["position"]))
{
    echo("<b>Position: </b>" . $_SESSION["position"]);
    echo("<br><br>");
}

if(isset($_SESSION["ITProject"]))
{
    echo("<b>IT Project: </b>" . $_SESSION["ITProject"]);
    echo("<br><br>");
}

?>

<html>

<body>

<?php include('Header.php'); ?>
<?php include('Menu.php'); ?>

<div class="content">

</div>

<?php include('Footer.php'); ?>


</body>
</html>

CodePudding user response:

First, make sure your session is saved before redirect, you can use session_write_close().

Second, make sure your script finish just after redirect, use exit.

You can use a function that makes both actions to economize code:

session_start();

if(isset($_POST["employeeNameBox"]))
{
    $_SESSION["employeeName"] = $_POST["employeeNameBox"];
    my_redirect('Session2.php');
}

//Here rest of vars

function my_redirect($to)
{
    session_write_close();
    header('Location: ' . $to);
    exit;
}
  • Related