1

I have a sample program that passes the inputs entered in the fields to a $_SESSION, then echos to a 3rd page. But it seems the inputs entered in the first page (index.php) does not echo, whereas in the 2nd page, it does. How do I fix that?

I also want to have a code that the inputs are destroyed with closed, that's why I have a onunload="<?php session_destroy(); ?>" in every body of my code.

index.php code:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form class="" method="post" action="page1indexHandler.php" onunload="<?php session_destroy(); ?>">
            Check-in: <input type="text" name="check_in" placeholder="Check-in">
            Check-out: <input type="text" name="check_out" placeholder="Check-out">
            <br>
            <input type="submit" name="Proceed" value="Proceed">
        </form>
    </body>
</html>

page1indexHandler.php code:

<?php  
session_start();
$_SESSION['check_in'] = $_POST['check_in'];
$_SESSION['check_out'] = $_POST['check_out'];
header("Location: register.php");
?>

register.php (second page) code:

<?php 
session_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form class="" method="post" action="page2registerHandler.php" onunload="<?php session_destroy(); ?>">
            first name: <input type="text" name="firstname">
            last name : <input type="text" name="lastname">
            <br>
            <input type="submit" name="proceed" value="proceed">
         </form>
    </body>
</html>

output.php (just for echo / checking purposes) code:

<?php 
    session_start();
    echo $_SESSION['check_in'] . "<br>";
    echo $_SESSION['check_out'] . "<br>";
    echo $_SESSION['firstname'] . "<br>";
    echo $_SESSION['lastname'] . "<br>";
?>

Here is the output of the sample program:

Notice: Undefined index: check_in in C:\xampp\htdocs\series\kenny\output.php on line 3


Notice: Undefined index: check_out in C:\xampp\htdocs\series\kenny\output.php on line 4

John // I inputted this in the register.php code, it echoes
Doe // also this one
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
Ken Flake
  • 585
  • 8
  • 28
  • 2
    `onunload` works on a client side. So `session_destroy` always runs on page load. – u_mulder Jan 03 '17 at 07:52
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – u_mulder Jan 03 '17 at 07:53
  • I see.. I tried removing them and it worked. Thanks! =) But I want to do something, for example, when the user closes the browser in the middle of inputting these forms, the completed ones would just be destroyed. How do I do that? – Ken Flake Jan 03 '17 at 07:58
  • @Kenny what do you mean by 'completed ones'? – Progrock Jan 03 '17 at 08:17
  • I mean, when I completed the `check in` and `check out ` input, then it proceeds, right? Then for example, I discontinued the `first name` and `last name` page and closed my browser. – Ken Flake Jan 03 '17 at 08:21

1 Answers1

2

I'm assuming here that your code does indeed flow through from your index.php to the handler and straight on to the register form.

The problem is, your form pages are destroying the sessions when they load. The PHP code inside your "onunload" event of the form is executed before the page is sent to the user. All PHP code is.

PHP is a server-side language, none of it is executed by the client. On the second page it works because it is not loading a page that immediately destroys the session.