如何在PHP中从前一页访问变量。

huangapple go评论62阅读模式
英文:

how to access a variable from a previous page in php

问题

我已经制作了一个表单,您可以在其中输入姓名并选中您喜欢的体育运动的复选框。
有两个按钮:

  1. 提交按钮:输出姓名和您已经选中的运动项目。
  2. 下一个按钮:将您发送到另一个页面,在该页面您需要选择您喜欢的菜肴。
    在下一个页面上,您可以选择您喜欢的菜肴,那里有一个返回按钮,可以将您发送回上一页。

基本上有两个文件。

第一个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 préféré : $recu_dish";
            echo "<br>";
        }
    }
    else if($_POST["submit_btn"]=='next'){
        header("Location: page2.php");
        exit();
    }  
?>

<html>
    <body>
        <h1>表单 1</h1>
        <div class="formbox">
            <form method="POST" action="">
                输入您的姓名 : <input type="text" name="username" placeholder="用户名"><br>
                选择一项运动:
                <input type="checkbox" name="sport" value="足球">足球
                <input type="checkbox" name="sport" value="排球">排球
                <input type="checkbox" name="sport" value="网球">网球
                <input type="checkbox" name="sport" value="游泳">游泳
                <br>   
                <input type="submit" class="button" name="submit_btn" value="提交">
                <input type="submit" class="button" name="submit_btn" value="下一页">
            </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>表单 2</h1>
        <div class="formbox">
            <form method="post" action="page1.php">
                选择一道菜:
                <input type="checkbox" name="dish" value="塔吉锅">塔吉锅
                <input type="checkbox" name="dish" value="库斯库斯">库斯库斯
                <input type="checkbox" name="dish" value="面条">面条
                <input type="checkbox" name="dish" value="烤鸡">烤鸡
                <br><input type="submit" class="button" name="btn" value="返回">
            </form>
        </div>
    </body>
</html>

理想情况下,当您返回到第一页时,它应该记住来自 page2.php 的菜肴输入以及上次的姓名和运动输入,以便在单击提交时显示结果。
然而,当我单击"返回"并返回到 page1 时,输入并没有保存。

我已经尝试使用 $_COOKIE$_SESSION,但似乎无法弄清楚。

英文:

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

&lt;?php
session_start();
if (isset($_POST[&quot;username&quot;]) and isset($_POST[&quot;sport&quot;])){
setcookie(&quot;name&quot;, $_POST[&quot;username&quot;], time() + 120);
setcookie(&quot;sport&quot;, $_POST[&quot;sport&quot;], time() + 120);
$recu_nom = $_COOKIE[&quot;name&quot;];
$recu_sport = $_COOKIE[&quot;sport&quot;];
}
if(isset($recu_nom) and isset($recu_sport)){
if($_POST[&quot;submit_btn&quot;]==&quot;submit&quot;){
echo &quot;donn&#233;es:&quot;;
echo &quot;&lt;br&gt;&quot;;
echo &quot;nom  : $recu_nom&quot;;
echo &quot;&lt;br&gt;&quot;;
echo &quot;sport pr&#233;f&#233;r&#233; : $recu_sport&quot;; 
echo &quot;&lt;br&gt;&quot;;
}
else if(isset($_COOKIE[&quot;dish&quot;]) and $_POST[&quot;submit_btn&quot;]==&quot;submit&quot;){
$recu_dish = $_COOKIE[&quot;dish&quot;];
echo &quot;donn&#233;es:&quot;;
echo &quot;&lt;br&gt;&quot;;
echo &quot;nom  : $recu_nom&quot;;
echo &quot;&lt;br&gt;&quot;;
echo &quot;sport pr&#233;f&#233;r&#233; : $recu_sport&quot;; 
echo &quot;&lt;br&gt;&quot;;
echo &quot;plat pref&#233;r&#233; : $recu_dish&quot;;
echo &quot;&lt;br&gt;&quot;;
}
}
else if($_POST[&quot;submit_btn&quot;]==&#39;next&#39;){
header(&quot;Location: page2.php&quot;);
exit();
}
?&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h1&gt;form 1&lt;/h1&gt;
&lt;div class = &quot;formbox&quot;&gt;
&lt;form method=&quot;POST&quot; action=&quot;&quot;&gt;
entrer votre nom : &lt;input type=&quot;test&quot;, name=&quot;username&quot;, placeholder=&quot;Nom d&#39;utilisateur&quot;&gt;&lt;br&gt;
choisir un sport :
&lt;input type=&quot;checkbox&quot; name=&quot;sport&quot;  value=&quot;foot&quot;&gt;Foot
&lt;input type =&quot;checkbox&quot; name=&quot;sport&quot; value=&quot;volley&quot;&gt;volley
&lt;input type=&quot;checkbox&quot; name=&quot;sport&quot; value=&quot;tennis&quot;&gt;tennis
&lt;input type=&quot;checkbox&quot; name=&quot;sport&quot; value=&quot;natation&quot;&gt;natation
&lt;br&gt;   
&lt;input type=&quot;submit&quot; class=&quot;button&quot; name=&quot;submit_btn&quot; value=&quot;submit&quot; &gt;
&lt;input type=&quot;submit&quot; class=&quot;button&quot; name=&quot;submit_btn&quot; value=&quot;next&quot; &gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

and a second file for the second page page2.php

&lt;?php 
session_start();
if(isset($_POST[&quot;dish&quot;]) and isset($_POST[&quot;btn&quot;])){
setcookie(&quot;dish&quot;, $_POST[&quot;dish&quot;], time() + 120, &quot;/&quot;);
}
?&gt;
&lt;html&gt;
&lt;body&gt;
&lt;h1&gt;form 2&lt;/h1&gt;
&lt;div class=&quot;formbox&quot;&gt;
&lt;form method=&quot;post&quot;, action=&quot;page1.php&quot;&gt;
choisir un plat :
&lt;input type=&quot;checkbox&quot; name=&quot;dish&quot; value=&quot;tajine&quot;&gt;tajine
&lt;input type =&quot;checkbox&quot; name=&quot;dish&quot; value=&quot;couscous&quot;&gt;couscous
&lt;input type=&quot;checkbox&quot; name=&quot;dish&quot; value=&quot;les pattes&quot;&gt;les pattes
&lt;input type=&quot;checkbox&quot; name=&quot;dish&quot; value=&quot;poullet grille&quot;&gt;poullet grille
&lt;br&gt;&lt;input type=&quot;submit&quot; class=&quot;button&quot; name=&quot;btn&quot; value=&quot;back&quot;&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

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.

答案1

得分: 0

你可以像上面提到的那样使用 $_SESSION[]。在 page1.php 中,你会想要获取用户名输入和复选框值的两个值,并将它们存储在 $_SESSION[] 中。然后在 page2.php 上执行相同的操作(从表单中移除动作并检查是否设置了 $_POST[btn],然后使用 header 返回到上一页)。然后回到 page1.php,你想要检查所有会话是否存在,如果存在,则打印它们供用户查看。

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 préfé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>
</html>
英文:

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

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

page2.php

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

</html>

huangapple
  • 本文由 发表于 2023年3月12日 06:32:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75709978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定