Search code examples
phpsessionpostsession-variables

What am I doing wrong with this phone SESSION I'm trying to print to screen?


I am testing a SESSION where the three prior pages prints to the last page. But I'm have trouble getting it to work. Everything goes well until the last page. Here is the code:

<?php
session_start();

if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
$_SESSION['age'] = $_POST ['age'];

echo $_SESSION['name'];
echo $_SESSION['email'];
echo $_SESSION['age'];
}
?>

I'm getting a blank or a index error for the session variable. Can anyone help with the correct way to print user inputted session data?

These are the 1st, 2nd and 3rd pages:

<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_1.php">
<input type="text" name="name" placeholder="enter name"></input>
<input type="submit"> next</input>
</form>
<?php
//Set session variables
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
}
?>
</body>
</html>


<?php
session_start();
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="test_2_page_2.php">
<input type="text" name="email" placeholder="enter email"></input>
<input type="submit"> next</input>
</form>
<?php
// Set session variables
?>
</body>
</html>


<?php
session_start();
if (isset($_POST['submit'])) { 
$_SESSION['name'] = $_POST ['name'];
$_SESSION['email'] = $_POST ['email'];
$_SESSION['age'] = $_POST ['age'];
}
?>
<html>
<head>
<title>phpsession</title>
</head>
<body>
<form method="POST" action="post_test.php">
<input type="text" name="age" placeholder="enter age"></input>
<input type="submit"> next</input>
</form>
<?php
// Set session variables
?>
</body>
</html>

Solution

  • The input element is self-closing in that you do not need </input> - rather the tag is simply closed using />

    To display Next as the submit button you would use the value attribute - ie: <input type='submit' value='Next' />

    Once the session variable is assigned you do not need to keep trying to set it and will encounter errors if the variable is not available in the POST array - so use isset to test prior to creating a variable

    <?php
        session_start();
        if ( isset( $_POST['submit'], $_POST['name'] ) ) { 
            $_SESSION['name'] = $_POST['name'];
        }   
    ?>
    <html>
        <head>
            <title>phpsession</title>
        </head>
        <body>
            <form method="POST" action="test_2_page_1.php">
                <input type="text" name="name" placeholder="enter name" />
                <input type="submit" name='submit' value='Next'>
            </form>
        </body>
    </html>
    
    
    
    
    
    <?php
        session_start();
        if ( isset( $_POST['submit'], $_POST['email'] ) ) {
            $_SESSION['email'] = $_POST['email'];
        }
    ?>
    <html>
        <head>
            <title>phpsession</title>
        </head>
        <body>
            <form method="POST" action="test_2_page_2.php">
                <input type="text" name="email" placeholder="enter email" />
                <input type="submit" name='submit' value='Next'>
            </form>
        </body>
    </html>
    
    
    
    
    
    
    
    <?php
        session_start();
        if( isset( $_POST['submit'],$_POST['age'] ) ) {
            $_SESSION['age'] = $_POST['age'];
        }
    ?>
    <html>
        <head>
            <title>phpsession</title>
        </head>
        <body>
            <form method="POST" action="post_test.php">
                <input type="text" name="age" placeholder="enter age" />
                <input type="submit" name='submit' value='Next'>
            </form>
        </body>
    </html>
    

    To further help you solve your issues I quickly put together a single page demo of getting and setting the session variables based upon form submissions.. hope it'll help

    <?php
        session_start();
        #https://stackoverflow.com/questions/54194496/what-am-i-doing-wrong-with-this-phone-session-im-trying-to-print-to-screen/54194890?noredirect=1#comment95217192_54194890
    
        if( !empty( $_GET['reset'] ) ){
            unset( $_SESSION['name'] );
            unset( $_SESSION['email'] );
            unset( $_SESSION['age'] );
            header( sprintf('Location: %s', $_SERVER['SCRIPT_NAME'] ) );
        }
    
        /* 
            as each form only submits two values ( submit being one ) we can
            simply remove it from the POST array and use the remaining field/value
            to generate the session variables using simple array techniques
    
            The below could easily be replaced with pre-defined variables, such as:
            if( isset( $_POST['name'] ) )$_SESSION['name']=$_POST['name']; etc
    
        */
        if ( isset( $_POST['submit'] ) ) {
            /* remove the submit button & value from the array */
            unset( $_POST['submit'] );
    
            /* there will now be one item, with index = 0 */
            $keys=array_keys( $_POST );
            $values=array_values( $_POST );
    
            /* set the session variable */
            $_SESSION[ $keys[0] ]=$values[0];
        }
    
    ?>
    <html>
        <head>
            <title>phpsession</title>
        </head>
        <body>
            <form method='POST'>
            <?php
                if( empty( $_SESSION['name'] ) ){
                    echo "<input type='text' name='name' placeholder='enter name' />";
                }
                if( !empty( $_SESSION['name'] ) && empty( $_SESSION['email'] ) ){
                    echo "<input type='text' name='email' placeholder='enter email address' />";
                }
                if( !empty( $_SESSION['name'] ) && !empty( $_SESSION['email'] ) && empty( $_SESSION['age'] ) ){
                    echo "<input type='text' name='age' placeholder='enter your age' />";
                }
                if( empty( $_SESSION['age'] ) ){
                    echo "<input type='submit' name='submit' value='Next'>";
                } else {
                    if( empty( $_POST['finish'] ) ) echo "<input type='submit' name='finish' value='Finish'>";
                }
                if( isset( $_POST['finish'] ) ){
                    /* 
                        Once the user has completed each form, send them off to their final destination?
                        or do something else....
                    */
                    printf(
                        'Do some interesting stuff now ....
                        <pre>%s</pre><a href="?reset=true">Reset</a>',
                        print_r( $_SESSION, true )
                    );
                }
            ?>
            </form>
        </body>
    </html>