英文:
how to access a variable from a previous page in php
问题
我已经制作了一个表单,您可以在其中输入姓名并选中您喜欢的体育运动的复选框。
有两个按钮:
- 提交按钮:输出姓名和您已经选中的运动项目。
- 下一个按钮:将您发送到另一个页面,在该页面您需要选择您喜欢的菜肴。
在下一个页面上,您可以选择您喜欢的菜肴,那里有一个返回按钮,可以将您发送回上一页。
基本上有两个文件。
第一个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 :
- submit button : outputs the name and the sport you've checked.
- 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.
答案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
<?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>
</html>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论