phphtmlformswebcookies

how to access a variable from a previous page in php


I have made a form where you input name and check a box of your favorite sport. there two buttons :

  1. submit button : outputs the name and the sport you've checked.
  2. next button : sends you to another page where you have to check your favorite dish. in the next page where you select the favorite dish there is a back button that sends you back to the previous page.

there are basically two files.

the first php file is for the first page page1.php

<?php
    session_start();
    
    if (isset($_POST["username"]) and isset($_POST["sport"])){
        setcookie("name", $_POST["username"], time() + 120);
        setcookie("sport", $_POST["sport"], time() + 120);
        $recu_nom = $_COOKIE["name"];
        $recu_sport = $_COOKIE["sport"];
    }
    if(isset($recu_nom) and isset($recu_sport)){
        if($_POST["submit_btn"]=="submit"){
            echo "données:";
            echo "<br>";
            echo "nom  : $recu_nom";
            echo "<br>";
            echo "sport préféré : $recu_sport"; 
            echo "<br>";
        }
        else if(isset($_COOKIE["dish"]) and $_POST["submit_btn"]=="submit"){
            $recu_dish = $_COOKIE["dish"];
            echo "données:";
            echo "<br>";
            echo "nom  : $recu_nom";
            echo "<br>";
            echo "sport préféré : $recu_sport"; 
            echo "<br>";
            echo "plat preféré : $recu_dish";
            echo "<br>";
        }
    }
    else if($_POST["submit_btn"]=='next'){
        header("Location: page2.php");
        exit();
    }
   

    
?>

<html>
    <body>
        <h1>form 1</h1>
        <div class = "formbox">
            <form method="POST" action="">
                entrer votre nom : <input type="test", name="username", placeholder="Nom d'utilisateur"><br>
                choisir un sport :
                <input type="checkbox" name="sport"  value="foot">Foot
                <input type ="checkbox" name="sport" value="volley">volley
                <input type="checkbox" name="sport" value="tennis">tennis
                <input type="checkbox" name="sport" value="natation">natation
                <br>   
                <input type="submit" class="button" name="submit_btn" value="submit" >
                <input type="submit" class="button" name="submit_btn" value="next" >
                
            </form>
        </div>
    </body>
</html>

and a second file for the second page page2.php

<?php 
    session_start();
    if(isset($_POST["dish"]) and isset($_POST["btn"])){
        setcookie("dish", $_POST["dish"], time() + 120, "/");
    }
?>

<html>
    <body>
        <h1>form 2</h1>
        <div class="formbox">
            <form method="post", action="page1.php">
                choisir un plat :
                <input type="checkbox" name="dish" value="tajine">tajine
                <input type ="checkbox" name="dish" value="couscous">couscous
                <input type="checkbox" name="dish" value="les pattes">les pattes
                <input type="checkbox" name="dish" value="poullet grille">poullet grille
                <br><input type="submit" class="button" name="btn" value="back">
            </form>
        </div>
    </body>
</html>

ideally when you return to the first page it should rememeber both the dish input from page2.php and the last name and sport input, so that when you click submit it shows the results. however when I click on back and go back back to page1 the input isn't saved.

I have tried working with $_COOKIE and $_SESSION but I just can't seem to figure it out.


Solution

  • You can use a $_SESSION[] as stated above. On page1.php you will want to get the values of both the username input as well as the checkbox value and store them in a $_SESSION[]. Then you will want to do the same on page2.php (Remove the action from the form and check if $_POST[btn] is set, then use header to go back to previous page). Then back on page1.php you want to check if all of the sessions exist, and if they do then print them for the user to see.

    page1.php

    <?php
    session_start();
     if(isset($_POST['submit_next'])){
        $_SESSION['sport'] = $_POST['sport'];
        $_SESSION['username'] = $_POST['username'];
        header("Location: page2.php");
        exit();
    }
    if(isset($_POST['submit_btn'])){
        unset($_SESSION['sport']);
        unset($_SESSION['username']);
        if(isset($_POST['sport'])){
            $_SESSION['sport'] = $_POST['sport'];
        }
        if(isset($_POST['username'])){
            $_SESSION['username'] = $_POST['username'];
        }
    }
    ?>
    <html>
    <body>
        <h1>form 1</h1>
        <div class = "formbox">
            <form method="POST" action="">
                entrer votre nom : 
                <input type="test", name="username" value="<?php if(isset($_POST['username']) && $_POST['username'] != ''){echo $_POST['username'];} else{if(isset($_SESSION['username']) && $_SESSION['username'] != ''){echo $_SESSION['username'];}} ?>" placeholder="Nom d'utilisateur"><br>
                choisir un sport :
                <input type="checkbox" name="sport" value="foot" <?php if(isset($_SESSION['sport']) && $_SESSION['sport'] == 'foot'){echo 'checked';} ?>>Foot
                <input type ="checkbox" name="sport" value="volley" <?php if(isset($_SESSION['sport']) && $_SESSION['sport'] == 'volley'){echo 'checked';} ?>>volley
                <input type="checkbox" name="sport" value="tennis" <?php if(isset($_SESSION['sport']) && $_SESSION['sport'] == 'tennis'){echo 'checked';} ?>>tennis
                <input type="checkbox" name="sport" value="natation" <?php if(isset($_SESSION['sport']) && $_SESSION['sport'] == 'natation'){echo 'checked';} ?>>natation
                <br>
                <input type="submit" class="button" name="submit_btn" value="submit" >
                <input type="submit" class="button" name="submit_next" value="next" >
            </form>
        </div>
        <?php
            if(isset($_POST['submit_btn'])){
        ?>
        <div class="row">
            <h5><?php if(isset($_POST['username']) && $_POST['username'] != ''){echo 'Nom: '.$_POST['username'];}  else{if(isset($_SESSION['username']) && $_SESSION['username'] != ''){echo 'Nom: '.$_SESSION['username'];}} ?></h5>
            
            <h5><?php if(isset($_POST['sport']) && $_POST['sport'] != ''){echo 'Sport préféré: '.$_POST['sport'];} else{if(isset($_SESSION['sport'])){echo 'Sport préféré: '.$_SESSION['sport'];}} ?></h5>
            
            <h5><?php if(isset($_SESSION['dish'])){echo 'Plat preféré: '.$_SESSION['dish']; unset($_SESSION['dish']);} ?></h5>
            
        </div>
        <?php
            }
        ?>
    </body>
    </html>
    

    page2.php

    <?php 
    session_start();
    if(isset($_POST['btn'])){
        if(isset($_POST["dish"])){
            $_SESSION['dish'] = $_POST['dish'];
            header("Location: page1.php");
            exit();
        }
    }
    ?>
    <html>
    <body>
        <h1>form 2</h1>
        <div class="formbox">
            <form method="post", action="">
                choisir un plat :
                <input type="checkbox" name="dish" value="tajine" >tajine
                <input type ="checkbox" name="dish" value="couscous">couscous
                <input type="checkbox" name="dish" value="les pattes">les pattes
                <input type="checkbox" name="dish" value="poullet grille">poullet grille
                <br><input type="submit" class="button" name="btn" value="back">
            </form>
        </div>
    </body>