简体   繁体   English

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

[英]Send PHP form data to a different page

I have just written this code in which I have a form.我刚刚编写了这段代码,其中有一个表格。 You have to write your name, surname and country.你必须写下你的名字、姓氏和国家。 You also have to choose your favourite colour.您还必须选择自己喜欢的颜色。 After that, you push a submit button so that you can see the data afterwards.之后,您按下提交按钮,以便之后可以查看数据。 I'm using the GET method with 1 page, but I have to use a second one with the POST method so that each echo is on that second page.我在 1 页上使用 GET 方法,但我必须在 POST 方法中使用第二个,以便每个回显都在第二页上。 How could I do that?我怎么能那样做? My code is:我的代码是:

<!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>

I know i have to use a link to that second page, but that's all I know.我知道我必须使用指向第二页的链接,但这就是我所知道的。 Thanks in advance!提前致谢!

In the form tag you can specify where to form gets submitted to:在表单标签中,您可以指定表单提交到的位置:

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

On that second page you get the values via the $_GET Variable like在第二页上,您可以通过 $_GET 变量获取值,例如

<?php
echo $_GET['name'].' '.$_GET['surname'];

If I understand correctly, the code you show is for Page 1 where the user can:如果我理解正确,您显示的代码适用于第 1 页,用户可以:

  1. Input data if data don't exist;如果数据不存在则输入数据;
  2. View data and confirm them if they exist.查看数据并确认它们是否存在。 At this point store data in SESSION and send the user to an other page.此时将数据存储在 SESSION 中并将用户发送到另一个页面。

To do so, remember you have to add the session_start() command right at the beginning of every page in which you will be able to manipulate Session Data.为此,请记住,您必须在能够操作 Session 数据的每个页面的开头添加 session_start() 命令。

 <?php
 session_start();

 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>";

         $_SESSION["name"] = $name;
         $_SESSION["surname"] = $surname;
         $_SESSION["country"] = $country;
         $_SESSION["colour"] = $colour;

         <a href="another_page.php">Confirm</a>

     }else 
         echo "<strong>Complete the blank spaces</strong>";
 }else{
 ?>
...

In "another_page.php" then you will find that you can access your data simply querying the $_SESSION array.在“another_page.php”中,您会发现只需查询 $_SESSION 数组即可访问您的数据。

<?php
session_start();
...
// Echo session variables that were set on previous page
echo "Name is " . $_SESSION["name"] . ".<br>";
echo "Surname is " . $_SESSION["surname"] . ".";
// etc.
?>

Complete reference is the PHP Manual and simple reference is in W3C PHP Sessions page.完整参考是PHP 手册,简单参考在W3C PHP 会话页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM