将 PHP 表单数据发送到不同的页面

我刚刚编写了这段代码,其中有一个表单。您必须写下您的名字、姓氏和国家。您还必须选择自己喜欢的颜色。之后,您按下提交按钮,以便之后可以查看数据。我在 1 页上使用 GET 方法,但我必须在 POST 方法中使用第二个方法,以便每个回显都在第二页上。我怎么能那样做?我的代码是:


<!DOCTYPE html>

<html>

 <head>

 <meta charset="UTF-8">

 <title>Form</title>


 <link rel="stylesheet" type="text/css" href="form.css">

 </head>

 <body>

 <section>

 <?php

 if (isset($_GET["name"])){

     $name = $_GET["name"];

     if ($name != ""){

         $surname = $_GET["surname"];

         $country = $_GET["country"];

         $colour = $_GET["colour"];


         echo "<p>";

         echo "<h2>Data</h2>";

         echo $name . " " . $surname . "</br />";

         echo $country . "<br />";

         echo $colour;

         echo "</p>";

     }else 

         echo "<strong>Complete the blank spaces</strong>";

 }else{

 ?>

 <h1>Form</h1>

 <form class="elegant" method="GET" action="?">

 <fieldset>

 <legend>Favourite colour</legend>


 <div>

 <label for="nombre">Name</label>

 <input type="text" placeholder="Name" name="name"

id="name" />

 </div>

 <div>

 <label for="surname">Surname</label>

 <input type="text" placeholder="Surname" name="surname"

id="surname" size="50" />

 </div>

 <div>

 <label for="country">Country</label>

 <input type="text" placeholder="Country" name="country" id="country"

size="10" maxlength="9" />

 </div>

 <div>

<select name="colour" id="colour">

<option value="yellow" <?php if ($colour == "yellow" ) echo "selected"  ?> >yellow</option>

<option value="red" <?php if ($colour == "red" ) echo "selected"  ?> >red</option>

 </div>


 <input class="btn" type="submit" value="Save" />

 </fieldset>

 </form>

 <?php

 }

 ?>


 </section>

 </body>

</html>

我知道我必须使用指向第二页的链接,但我只知道这些。提前致谢!


白猪掌柜的
浏览 95回答 3
3回答

jeck猫

在第二页上,您通过 $_GET 变量获取值,例如<?phpecho $_GET['name'].' '.$_GET['surname'];

九州编程

在表单标签中,您可以指定表单提交到的位置:<form&nbsp;class="elegant"&nbsp;method="GET"&nbsp;action="YOUR_PAGE_URL_HERE">

30秒到达战场

如果我理解正确,您显示的代码是针对第 1 页的,用户可以在其中:如果数据不存在则输入数据;查看数据并确认它们是否存在。此时将数据存储在 SESSION 中,并将用户发送到另一个页面。为此,请记住您必须在每个页面的开头添加 session_start() 命令,您将能够在其中操作会话数据。<?php&nbsp;session_start();&nbsp;if (isset($_GET["name"])){&nbsp; &nbsp; &nbsp;$name = $_GET["name"];&nbsp; &nbsp; &nbsp;if ($name != ""){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$surname = $_GET["surname"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$country = $_GET["country"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$colour = $_GET["colour"];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<p>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<h2>Data</h2>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $name . " " . $surname . "</br />";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $country . "<br />";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $colour;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "</p>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$_SESSION["name"] = $name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$_SESSION["surname"] = $surname;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$_SESSION["country"] = $country;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$_SESSION["colour"] = $colour;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="another_page.php">Confirm</a>&nbsp; &nbsp; &nbsp;}else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<strong>Complete the blank spaces</strong>";&nbsp;}else{&nbsp;?>...在“another_page.php”中,您会发现只需查询 $_SESSION 数组即可访问您的数据。<?phpsession_start();...// Echo session variables that were set on previous pageecho "Name is " . $_SESSION["name"] . ".<br>";echo "Surname is " . $_SESSION["surname"] . ".";// etc.?>完整参考是PHP 手册,简单参考是在W3C PHP 会话页面。
打开App,查看更多内容
随时随地看视频慕课网APP