html 如何在php中访问前一页的变量

huus2vyu  于 2023-03-16  发布在  PHP
关注(0)|答案(1)|浏览(135)

我做了一个表单,您可以输入姓名,并选中您最喜欢的运动。有两个按钮:
1.提交按钮:输出您选中的名称和运动。
1.下一步按钮:发送你到另一个页面,你必须检查你最喜欢的菜。在下一页,你选择最喜欢的菜有一个后退按钮,发送你回到前一页。
基本上有两个文件。
第一个php文件用于第一个页面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>

以及用于第二页面的第二文件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>

理想情况下,当你返回到第一页时,它应该记住从page2.php输入的菜和输入的姓和运动,这样当你点击提交时,它会显示结果。然而,当我点击返回并返回到page 1时,输入没有保存。
我试过使用$_COOKIE和$_SESSION,但似乎就是弄不明白。

aurhwmvo

aurhwmvo1#

你可以如上所述使用一个**$_SESSION[]。在page1.php上你会想要得到用户名输入值和复选框值,并将它们存储在一个$_SESSION[]中。然后你会想要在page2.php上做同样的事情(从表单中删除操作并检查是否设置了$_POST[btn],然后使用header返回上一页).然后返回page1.php**您要检查是否所有会话都存在,并且如果它们存在,则打印它们以供用户查看。

第一页. 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>

第二页. 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>

相关问题