I'm struggling to create a two-paged form using $_SESSION. What I want to achieve is the first page (page1.php) requires the user to enter his/her email address. And the second page (page2.php) requires the user to enter his/her password.

When the user submits page1.php, it takes you to page2.php, where the email address submitted will be printed. But unfortunately, the email address is not printed, as intended.

Please, note I've tried to adopt related resolved threads, but I'm still missing something.

The following is my code:
page1.php

<?php
session_start();
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>2 Step Login - Page 1</title>
<link href="page1.css" rel="stylesheet">
</head>

<body>

<div id="formwrap">
<div id="form_inner">
    <div id="logo">
      <img src="" alt="logo">
    </div>
    <div id="email">

    </div>
  <div id="pwd">
      Sign in
  </div>
  <div id="form">
    <form action="page2.php" method="post" enctype="multipart/form-data" name="form">

<?php

    //On page 1
    $_SESSION['username'] = $var_value;
?>
      <input id="username" name="username" type="text" placeholder="Email" autofocus required>

    <div id="forgot">No Yet A Member, Register Here</a></div>
    <input type="hidden" name="username" value="var_value">
    <input id="send" name="submit" type="submit" value="Next">
    </form>
  </div>
  </div>
</div>
</body>
</html>

page2.php

<?php
session_start();
?>
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>2 Step Login - Page 2</title>
<link href="page2.css" rel="stylesheet">
</head>

<body>

<div id="formwrap">
<div id="form_inner">
    <div id="logo">
      <img src="" alt="logo">
    </div>
    <div id="email">
<?php
    //On age 2
    $var_value = $_SESSION['username'];
    echo $_SESSION['username'];
?>
    </div>
  <div id="pwd">
      Enter password
  </div>
  <div id="form">
    <form action="login.php" method="post" enctype="multipart/form-data" name="form">
      <input id="password" name="password" type="password" placeholder="Password" autofocus>
      <div id="chkbx">
<div id="inptch">
  <input id="keep_signed_in" name="keep_signed_in" type="checkbox" value="">
</div>
Keep me signed in</div>

    <div id="forgot">I Forgot My Password</div>
    <div id="different_account">Not A Member, Register Here</div>
    <input type="hidden" name="username" value="var_value">
    <input type="hidden" name="password" value="var_value">
    <input id="send" name="submit" type="submit" value="Sign in">
    </form>
  </div>
</div>
</div>
</body>
</html>

Your help will be much appreciated. Thanks in advanced.

Recommended Answers

All 2 Replies

I tend to use var_dump($_SESSION['username']); to show me any returned values. You can add a input text to also see teh return, the code looks fine -

<div id="email">
    <input type="text" value="
        <?php
    //On age 2
        $var_value = $_SESSION['username'];
        echo $_SESSION['username'];
        ?>
    />
?>
    </div>

@AndreRet , Thanks, I've managed to fix the issue.

commented: Awesome, please share your answer with us if possible and mark this as solved, thanks. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.