如何在 PHP 中的页面之间传递数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1179559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass data between pages in PHP?
提问by MrChrister
In a nutshell on "page1.php" I have a calculator that consists of an HTML form, and then the PHP code totals the input and displays the total price. Below the price, it also displays a link to "page2.php" which contains an HTML form where they can enter their contact information. Upon submitting the form the selections they made on "page1.php" in the pricing calculator as well as the contact info on "page2.php" are emailed to me, and they are redirected to the home page.
简而言之,关于“page1.php”,我有一个由 HTML 表单组成的计算器,然后 PHP 代码对输入进行总计并显示总价。在价格下方,它还显示一个指向“page2.php”的链接,其中包含一个 HTML 表单,他们可以在其中输入他们的联系信息。提交表单后,他们在定价计算器中的“page1.php”上所做的选择以及“page2.php”上的联系信息将通过电子邮件发送给我,并将它们重定向到主页。
In the email that is submitted to me, I receive the contact info from "page2.php", but I don't receive anything from "page1.php", so the variables are not getting correctly passed. In addition to the PHP on each page, I am using hidden values in an HTML form on "page2.php" to echo the data that was entered in the HTML form on "page1.php". I know that one of my issues is that I have a couple of $_GETfields when my form is "post".
在提交给我的电子邮件中,我收到了来自“page2.php”的联系信息,但我没有收到来自“page1.php”的任何信息,因此变量没有被正确传递。除了每个页面上的 PHP 之外,我还在“page2.php”上的 HTML 表单中使用隐藏值来回显在“page1.php”上的 HTML 表单中输入的数据。我知道我的问题之一是$_GET当我的表单是“发布”时我有几个字段。
However when I change it so that everything is $_POST, the calculator no longer works. I tried to put this altogether with different snippets of code suggested by others. The form on "page1.php" has 13 fields, named "one" - "thirteen". $total display the values of 1-13.
但是,当我更改它以便一切正常时$_POST,计算器不再起作用。我试图将其与其他人建议的不同代码片段一起放在一起。“page1.php”上的表单有13个字段,命名为“one”-“13”。$total 显示 1-13 的值。
<?php
$submit = $_GET['submit'];
if($submit == "true")
{
$total = ($_POST['one'] + $_POST['two'] + $_POST['three'] + $_POST['four'] +
$_POST['five'] + $_POST['six'] + $_POST['seven'] + $_POST['eight']+ $_POST['nine'] +
$_POST['ten']+ $_POST['eleven'] + $_POST['twelve']+ $_POST['thirteen']);
echo " Your Price is $ " .number_format ($total, 2, '.', ','). "<BR>";
echo ('">Get Your Project Started</a>');
}
?>
The second form uses hidden values to echo the info from page1.php, and has three more fields named "name", "email" and "details".
第二个表单使用隐藏值来回显 page1.php 中的信息,并且还有三个名为“name”、“email”和“details”的字段。
<?php
$to = "[email protected]";
$message = "Pages:\t$_POST[one]\n";
$message .= "Pages:\t$_POST[two]\n";
$message .= "Pages:\t$_POST[three]\n";
$message .= "Ecommerce:\t$_POST[four]\n";
$message .= "No Ecommerce:\t$_POST[five]\n";
$message .= "CMS:\t$_POST[six]\n";
$message .= "No CMS:\t$_POST[seven]\n";
$message .= "Audio or Video:\t$_POST[eight]\n";
$message .= "Flash Intro:\t$_POST[nine]\n";
$message .= "Image Gallery:\t$_POST[ten]\n";
$message .= "Graphic Design or Logo:\t$_POST[eleven]\n";
$message .= "Copy:\t$_POST[twelve]\n";
$message .= "Images:\t$_POST[thirteen]\n";
$message .= "Price Total:\t$_POST[total]\n";
$message .= "Name:\t$_POST[name]\n";
$message .= "Email:\t$_POST[email]\n";
$message .= "\n";
$message .= "\n";
$message .= "Details:\t$_POST[details]\n";
mail($to, $subject, $message, $headers) ;
}
?>
So what would be the correct PHP to put on "page1.php" and "page2.php"? Sorry the code is such a mess, if anyone could point me in the right direction, that would be great.
那么放在“page1.php”和“page2.php”上的正确PHP是什么?抱歉,代码太乱了,如果有人能指出我正确的方向,那就太好了。
回答by MrChrister
PHP is stateless unless you save things in session (which isn't secure right?)
PHP 是无状态的,除非您在会话中保存内容(这不安全,对吗?)
You would need to make page2.php read all the values from page1.php and store them either in a server side state (session) or a client state (cookies or maybe hidden fields in the form)
您需要让 page2.php 读取 page1.php 中的所有值并将它们存储在服务器端状态(会话)或客户端状态(cookies 或表单中的隐藏字段)
If you want any of this secure or a secret, then you have to consider all of that as well. Nothing I explained is secret or secure in any way.
如果您想要任何安全或秘密,那么您也必须考虑所有这些。我所解释的一切都不是秘密或安全的。
EDIT:here is an example of page1.php that sends the values of page1.php to page2.php as get parameters. You can do this with hidden fields, cookies or sessions as well.
编辑:这里是 page1.php 的一个例子,它将 page1.php 的值作为获取参数发送到 page2.php。您也可以使用隐藏字段、cookie 或会话来执行此操作。
What is important to remember is that page2.php is totally unaware of page1.php, and can't get to the values like you could it forms programming. Each page starts and ends it's life by the time you see a full web page, so you have to use some extra tricks to keep values. Those tricks are sessions, cookies, form posts, or gets.
重要的是要记住 page2.php 完全不知道 page1.php,并且无法像编程那样获取值。当你看到一个完整的网页时,每个页面开始和结束它的生命,所以你必须使用一些额外的技巧来保持价值。这些技巧是会话、cookie、表单帖子或获取。
<html>
<head>
<title>Page 1</title>
</head>
<body>
<?php
//set defaults assuming the worst
$total = 0;
$one =0;
$two=0;
//verify we have that value in $__POST
if (isset($_POST['submit']))
{
$submit = $_POST['submit'];
//If it is true, try some math
if($submit == "sub-total")
{
if (isset($_POST['one']))
{
$one = $_POST['one'];
//Add checks to see if your values are numbers
if ( ! is_numeric($one)) { $one = 0;}
}
if (isset($_POST['two']))
{
$two = $_POST['two'];
if ( ! is_numeric($two)) { $two = 0;}
}
$total = $one + $two;
echo " Your Price is $ " .number_format ($total, 2, '.', ','). "<BR>";
}
if($submit == "submit" )
{
//go to page two, with the total from page1.php passed as a $__GET value
header("Location: page2.php?total=".$total);
}
}
?>
<form method="post" action="page1.php?submit=true">
<input type="text" name="one" id="one" value="<?=$one?>"/>
<input type="text" name="two" id="two" value="<?=$two?>"/>
<input type="submit" name="submit" value="sub-total" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
回答by Brian Ramsay
You can try using $_REQUESTinstead of $_GETor $_POST- it works for both. Also, if you put <input type='hidden'in the form on page 2 with your variables posted from page 1 they should get passed right along.
您可以尝试使用$_REQUEST代替$_GET或$_POST- 它适用于两者。此外,如果您将<input type='hidden'第 1 页上发布的变量放入第 2 页的表单中,它们应该会被直接传递。
回答by Robert Greiner
your php pages don't have state, so you have to get the posted variables, and send them along via hidden input fields.
您的 php 页面没有状态,因此您必须获取已发布的变量,并通过隐藏的输入字段发送它们。
<input type="hidden".....
You could also use sessions ,but it is a bit hackish to use them with forms if you ask me.
你也可以使用 session ,但如果你问我,将它们与表单一起使用有点hackish。
回答by Thomas
It depends on the methodattribute of your HTML formhow the variables get passed to your PHP script.
这取决于method您的 HTML 属性form如何将变量传递给您的 PHP 脚本。
In this case, it sounds like page1.phpdoes not have any side effect, and just passes on values, so the form on page1.phpshould use method=get, and the code of page2.phpthat reads these fields can find them in $_GET.
在这种情况下,听起来好像page1.php没有任何副作用,只是传递值,所以表单上page1.php应该使用method=get,page2.php读取这些字段的代码可以在$_GET.
The Submit button on page2.php(presumably) does have side effects on the server side, so it should use method=post, which you can read back from $_POSTon the server side.
page2.php(大概)上的提交按钮确实对服务器端有副作用,因此它应该使用method=post,您可以$_POST在服务器端读取它。
Also, have a look at the HTML source of page2.phpafter filling out the first page, to see if the values are passed correctly.
另外,看看page2.php填写完第一页后的HTML源代码,看看值是否正确传递。
回答by cregox
little late on the answer, yes, best way is using type=hiddenas others have mentioned already.
答案迟到了,是的,最好的方法是使用type=hidden其他人已经提到的方法。
page1 sends _POST to page2, which "saves" all of it in hidden fields, so it can send everything together to the final (or another) page.
page1 将 _POST 发送到 page2,后者将所有内容“保存”在隐藏字段中,因此它可以将所有内容一起发送到最终(或另一个)页面。
but more importantly, and going to a broader-topic, is considering why using pages at allto divide a form!
但更重要的是,进入一个更广泛的主题,正在考虑为什么要使用页面来划分表单!
sorry for touching this subject here, I just felt like giving my 2 cents.
很抱歉在这里触及这个主题,我只是想给我的 2 美分。
don't page forms
不要分页表格
I'd say best thing is avoid paging as much as possible and only good reasons to do it so would be if you need server validation andbackwards compatibility with browsers that won't accept javascript.
我想说最好的办法是尽可能避免分页,只有在您需要服务器验证和向后兼容不接受 javascript 的浏览器时才这样做。
use newer resources instead
改用较新的资源
layout and design can both be achieved using javascriptand CSS, which are both meant for that. even if you do the user to "press next", use dhtmland all in 1 file. need server validation? ajax.
布局和设计都可以使用javascript和CSS来实现,它们都是为此而设计的。即使您让用户“按下一步”,也请使用dhtml和 1 个文件中的所有内容。需要服务器验证吗?阿贾克斯。
unless you're using HTML frames (which you probably shouldn't), loading a whole page is a redundant and unnecessary server and client load that takes more time in both using and programming (to take care of passing form info from one place to another).
除非您使用 HTML 框架(您可能不应该这样做),否则加载整个页面是一种冗余且不必要的服务器和客户端负载,这会在使用和编程方面花费更多时间(负责将表单信息从一个地方传递到另一个地方)其他)。
and it's far from being the best way to save the state of what's being done. granted, this is a whole complicated subject on its own, but again, ajax would be best here.
并且这远不是保存正在执行的状态的最佳方式。当然,这本身就是一个复杂的主题,但同样,ajax 在这里最好。

