0

I don't get the PHP $_SESSION working when I try to echo the result on the first page. The error message states basically that the variables are undefined.

The path i want to use:

  1. 1st page = form 1
  2. 2nd page = form 2
  3. Go back to 1st page = form 1 with all input filled from previous submit + all data from the 2 forms in a text.

Is that possible ?

Page 1 = index.php:

    <?php session_start(); ?>
    <!DOCTYPE html>
    <html lang="fr">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    </head>
    
    <body>   
        <form action="overview.php" id="regForm" method="post">
            
            <div class="tab">
                <h2>1. Your info</h2>
                <p><input type="text" placeholder="Prénom"  name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
                <p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
                <input type="submit" value="Valider">
            </div>
        </form>
    <?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>
    </body>
    </html>

Page 2 = overview.php:

<?php session_start(); ?>

<?php

   $_SESSION['fname'] = $_POST['fname'];
   $_SESSION['name'] = $_POST['name'];

?>

<form action="index.php" id="regForm" method="post">   
        <h2>1. Tes informations personnelles</h2>
        <p><input type="text" placeholder="e-mail" name="email"></p>
        <input type="submit" value="Valider">
</form>

Back to Page 1 = index.php:

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
</head>

<body>   
    <form action="overview.php" id="regForm" method="post">
        
        <div class="tab">
            <h2>1. Your info</h2>
            <p><input type="text" placeholder="Prénom"  name="fname" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
            <p><input type="text" placeholder="Nom" name="name" value="<?php echo htmlspecialchars($_SESSION['name']); ?>"></p>
            <input type="submit" value="Valider">
        </div>
    </form>

<?php echo "Your name is :",$_SESSION['name'], "and your first-name is ", $_SESSION['fname'];?>
<?php echo "Your e-mail is :", $_SESSION['email'] ;?>

</body>
</html>

Do you guys see any issue that prevents the code to run ?

Hymed Ghenai
  • 199
  • 15
  • 1
    Of course none of those values is set when you visit the index.php for the first time. – CBroe Jul 27 '20 at 12:43
  • @CBroe do you mean that i should add $_SESSION['fname'] = $_POST['fname']; $_SESSION['name'] = $_POST['name']; to the index ? – Hymed Ghenai Jul 27 '20 at 12:47
  • 1
    Btw it's fine if I don't get the value at the first page visit. But i do want to get them after the two submits ... you see what I mean ? – Hymed Ghenai Jul 27 '20 at 12:52
  • 1
    No, `$_POST['fname']` does not exist either, when you call the index.php for the first time. You should _check_ if those entries are set in the session, before you try to access them. – CBroe Jul 27 '20 at 12:55
  • 1
    @CBroe thanks for the hint ! Do you know how I could "check if those entries are set in the session" ? – Hymed Ghenai Jul 27 '20 at 13:08
  • https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined – CBroe Jul 27 '20 at 13:13
  • @CBroe i'm not sure this would help me on my issue. I don't think you got my point btw. The issue lies in the fact that none of the variables are stored while the php $_SESSION is being used ... even if I use 3 different pages, none of the variables are stored. Could you help me further ? – Hymed Ghenai Jul 27 '20 at 13:23
  • Do a bit of basic debugging then… Use var_dump to see what data you actually received in $_POST, and what $_SESSION actually contains, at various points. Also, go and check if proper PHP error reporting is enabled, if not, go and do that first of all. – CBroe Jul 27 '20 at 13:30

1 Answers1

0

Just found out the "WHY". I basically blocked cookies when I started the project which prevent $_sessions to work properly. So make guys sure that cookies are enabled on your website. The kind of things that waste an hour of work ...

Hymed Ghenai
  • 199
  • 15