简体   繁体   English

如何在PHP中的页面之间传递数据?

[英]How do I pass data between pages in PHP?

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. 简而言之,在“page1.php”中我有一个由HTML表单组成的计算器,然后PHP代码总计输入并显示总价。 Below the price, it also displays a link to "page2.php" which contains an HTML form where they can enter their contact information. 在价格之下,它还会显示“page2.php”的链接,其中包含一个HTML表单,可以输入他们的联系信息。 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”上做出的选择以及“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. 在提交给我的电子邮件中,我收到了来自“page2.php”的联系信息,但我没有从“page1.php”收到任何内容,因此变量未正确传递。 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". 除了每页上的PHP之外,我在“page2.php”上的HTML表单中使用隐藏值来回显在“page1.php”上的HTML表单中输入的数据。 I know that one of my issues is that I have a couple of $_GET fields when my form is "post". 我知道我的一个问题是,当我的表单是“post”时,我有几个$_GET字段。

However when I change it so that everything is $_POST , the calculator no longer works. 但是,当我更改它以便一切都是$_POST ,计算器不再有效。 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". “page1.php”上的表单有13个字段,名为“one” - “十三”。 $total display the values of 1-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 = "jessica@designs.com";
  $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"? 那么什么是正确的PHP放在“page1.php”和“page2.php”? Sorry the code is such a mess, if anyone could point me in the right direction, that would be great. 对不起代码是如此混乱,如果有人能指出我正确的方向,那将是伟大的。

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中的所有值,并将它们存储在服务器端状态(会话)或客户端状态(cookie或表单中的隐藏字段)

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. 编辑:这是page1.php的一个示例,它将page1.php的值作为get参数发送到page2.php。 You can do this with hidden fields, cookies or sessions as well. 您也可以使用隐藏字段,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. 重要的是要记住,page2.php完全没有意识到page1.php,并且无法获得像它可以形成编程的值。 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. 这些技巧是会话,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>

You can try using $_REQUEST instead of $_GET or $_POST - it works for both. 您可以尝试使用$_REQUEST而不是$_GET$_POST - 它适用于两者。 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. 此外,如果您在第2页的表单中<input type='hidden' ,并且您的变量已从第1页发布,那么它们应该正确传递。

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. 您也可以使用会话,但如果您问我,将它们与表格一起使用会有些不自在。

It depends on the method attribute of your HTML form how the variables get passed to your PHP script. 它取决于HTML formmethod属性如何将变量传递给PHP脚本。

In this case, it sounds like page1.php does not have any side effect, and just passes on values, so the form on page1.php should use method=get , and the code of page2.php that reads these fields can find them in $_GET . 在这种情况下,听起来像page1.php没有任何副作用,只是传递值,所以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 $_POST on the server side. page2.php上的Submit按钮(推测)确实在服务器端有副作用,因此它应该使用method=post ,你可以从服务器端的$_POST读回来。

Also, have a look at the HTML source of page2.php after filling out the first page, to see if the values are passed correctly. 另外,在填写第一页后,查看page2.php的HTML源代码,以查看值是否正确传递。

little late on the answer, yes, best way is using type=hidden as 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 all to 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 and backwards compatibility with browsers that won't accept javascript. 我说最好的事情是尽可能避免分页,只有很好的理由才能做到这一点,如果你需要服务器验证向后兼容不接受javascript的浏览器。

use newer resources instead 使用更新的资源

layout and design can both be achieved using javascript and CSS , which are both meant for that. 布局和设计都可以使用javascriptCSS实现,这两者都是为了这个。 even if you do the user to "press next", use dhtml and all in 1 file. 即使你让用户“按下”,也可以使用dhtml和all in 1文件。 need server validation? 需要服务器验证? ajax . 阿贾克斯

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在这里也是最好的。

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

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