PHP Variables get overwritten to Null

I’m having issues maintaining variables in the Session. I’ve confirmed that the same Session ID persists through page navigation, and I’ve also used the Print_r($_SESSION) to monitor the variables.

I’m using four pages.

  • index.php
  • custinfo.php
  • custbilling.php
  • confirm.php

On the first page I’m using a form to send data to the next page.

        <form name="prescreen" action="custinfo.php" method="post">
        <label>From DIA</label>
        <input name="startlocation" id="fromdia" type="radio" value="From DIA">
        <label>To DIA</label>
        <input name="startlocation" id="todia" type="radio" value="To DIA">
        <label>Choose Location:</label>
        City:<input name="city" id="city" type="text" />
        <span>or</span><br />
        Zipcode:<input name="zipcode" id="zipcode" type="text" />
    <h3>When do you need picked up?</h3>
        <label>Choose Date:</label>
        <input name="date" id="date" type="datetime-local" />
        <label>Choose Time:</label>
        <input name="time" id="time" type="time" />
    <input type="submit"  value="Get a Ride Now!" class="textbtn"></input>

On custinfo.php, I then use this in the head of the document:


    $_SESSION['testvar'] = 'THIS IS A TEST';
    $startlocation = $_POST["startlocation"];
    $city = $_POST["city"];
    $zipcode = $_POST["zipcode"];
    $date = $_POST["date"];
    $time = $_POST["time"];

    //Assign variables to the Session
    $_SESSION['startlocation'] = $startlocation;
    $_SESSION['city'] = $city;
    $_SESSION['zipcode'] = $zipcode;
    $_SESSION['date'] = $date;
    $_SESSION['time'] = $time;


The variables are properly read in and stored in the array. Then I use this form in the custinfo.php page:

        <form name="customerinfo" action="custbilling.php" method="post">
        <label>Contact Name</label>
        <input name="contactname" id="contactname" type="text">
        <label>Contact Email</label>
        <input name="contactemail" id="contactemail" type="text">
        <label>Contact Phone</label>
        <input name="contactphone" id="contactphone" type="text" />
        if($_SESSION['startlocation'] == "From DIA")
            echo '<hr />';
            echo '<label><b>To Location:</b></label>';
            echo 'Address1:<input name="toaddress1" id="toaddress1" type="text" />';
            echo 'Address2:<input name="toaddress2" id="toaddress2" type="text" />';
            echo 'City:<input name="tocity" id="tocity" type="text" />';
            echo 'Zip:<input name="tozip" id="tozip" type="text"  />';
            echo '<hr />';
            echo '<hr />';
            echo '<label><b>From Location</b></label>';
            echo 'Address1:<input name="fromaddress1" id="fromaddress1" type="text" />';
            echo 'Address2:<input name="fromaddress2" id="fromaddress2" type="text" />';
            echo 'City:<input name="fromcity" id="fromcity" type="text" />';
            echo 'Zip:<input name="fromzip" id="fromzip" type="text" />';
            echo '<hr />';


        <input type="submit"  value="Book Your Ride!" class="textbtn"></input>

In the head of the custbilling.php page I have everything pulling in like so:


/*Vars from Customer Info */
$contactname = $_POST['contactname'];
$contactemail = $_POST['contactemail'];
$contactphone = $_POST['contactphone'];
if($_SESSION['startlocation'] == "To DIA")
    $address1 = $_POST['fromaddress1'];
    $address2 = $_POST['fromaddress2'];
    $city = $_POST['fromcity'];
    $zipcode = $_POST['fromzip'];
    $address1 = $_POST['toaddress1'];
    $address2 = $_POST['toaddress2'];
    $city = $_POST['tocity'];
    $zipcode =$_POST['tozip'];

//Assign Variables to the Session
$_SESSION['contactname'] = $contactname;
$_SESSION['contactemail'] = $contactemail;
$_SESSION['contactphone'] = $contactphone;
$_SESSION['city']= $city;
$_SESSION['zipcode'] = $zipcode;


At this point, I’m displaying the information like so:\

<h1><?php echo $_SESSION['testvar']; ?></h1>
    <h2>Travel Information</h2>
    <h3>Please fill out this form:</h3>
    <p>Direction of Travel: <?php echo $_SESSION['startlocation']; ?></p>
    <p>LocationTo: <?php echo $_SESSION['city'] , $_SESSION['zipcode']; ?></p>
    <p>Date: <?php echo $_SESSION['date']; ?></p>
    <p>Time: <?php echo $_SESSION['time']; ?></p>
    <p>Customer Name: <?php echo $_SESSION['contactname']; ?></p>
    <p>Customer Email: <?php echo $_SESSION['contactemail']; ?></p>
    <p>Customer Phone: <?php echo $_SESSION['contactphone']; ?></p>
    <p>Address Information: <br />
        <span>Address 1:</span><?php echo $_SESSION['address1']; ?><br />
        <span>Address 2:</span><?php echo $_SESSION['address2']; ?><br />
        <span>City:</span><?php echo $_SESSION['city']; ?><br />
        <span>ZipCode:</span><?php echo $_SESSION['zipcode']; ?><br />

But the startlocation and all the variables from the first post that were saved in the array are now erased. You can also see the testvariable that I used to test that the session was working, and my $_SESSION['testvar'] Displays properly on ALL of the files. I initially had the code set like so $_SESSION['varname] = $_POST['varname'];, but that produced the same issue. So the variable would go to the next page, but it wouldn’t continue to the third page.

Any help would be greatly appreciated. Thank you.

Leave a Reply


Hire Me
Follow Me!
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.