如何在 PHP 中的页面之间传递数据? [英] How do I pass data between pages in PHP?

查看:26
本文介绍了如何在 PHP 中的页面之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,page1.php"我有一个由 HTML 表单组成的计算器,然后 PHP 代码对输入进行总计并显示总价.在价格下方,它还显示一个指向page2.php"的链接,其中包含一个 HTML 表单,他们可以在其中输入他们的联系信息.提交表单后,他们在定价计算器中的page1.php"上所做的选择以及page2.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. 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.

在提交给我的电子邮件中,我收到了来自page2.php"的联系信息,但我没有收到来自page1.php"的任何信息,因此变量没有被正确传递.除了每个页面上的 PHP 之外,我还在page2.php"上的 HTML 表单中使用隐藏值来回显在page1.php"上的 HTML 表单中输入的数据.我知道我的问题之一是当我的表单发布"时我有几个 $_GET 字段.

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 $_GET fields when my form is "post".

然而,当我改变它以便一切都是 $_POST 时,计算器不再工作.我试图将其与其他人建议的不同代码片段一起放在一起.page1.php"上的表单有13个字段,命名为one"-13".$total 显示 1-13 的值.

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.

<?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>');
  }
?>

第二个表单使用隐藏值来回显 page1.php 中的信息,并且还有三个名为name"、email"和details"的字段.

The second form uses hidden values to echo the info from page1.php, and has three more fields named "name", "email" and "details".

<?php
  $to = "jessica@designs.com";
  $message = "Pages:	$_POST[one]
";
  $message .= "Pages:	$_POST[two]
";
  $message .= "Pages:	$_POST[three]
";
  $message .= "Ecommerce:	$_POST[four]
";
  $message .= "No Ecommerce:	$_POST[five]
";
  $message .= "CMS:	$_POST[six]
";
  $message .= "No CMS:	$_POST[seven]
";
  $message .= "Audio or Video:	$_POST[eight]
";
  $message .= "Flash Intro:	$_POST[nine]
";
  $message .= "Image Gallery:	$_POST[ten]
";
  $message .= "Graphic Design or Logo:	$_POST[eleven]
";
  $message .= "Copy:	$_POST[twelve]
";
  $message .= "Images:	$_POST[thirteen]
";
  $message .= "Price Total:	$_POST[total]
";
  $message .= "Name:	$_POST[name]
";
  $message .= "Email:	$_POST[email]
";
  $message .= "
";
  $message .= "
";
  $message .= "Details:	$_POST[details]
";
  mail($to, $subject, $message, $headers) ;
  }
?>

那么,放在page1.php"和page2.php"上的正确 PHP 是什么?抱歉代码太乱了,如果有人能指出我正确的方向,那就太好了.

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.

推荐答案

PHP 是无状态的,除非你在会话中保存东西(这不安全吧?)

PHP is stateless unless you save things in session (which isn't secure right?)

您需要让 page2.php 读取 page1.php 中的所有值并将它们存储在服务器端状态(会话)或客户端状态(cookies 或表单中的隐藏字段)

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)

如果您想要任何安全或秘密,那么您也必须考虑所有这些.我所解释的一切都不是秘密或安全的.

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.

这里是一个 page1.php 的例子,它将 page1.php 的值作为获取参数发送到 page2.php.您也可以使用隐藏字段、cookie 或会话来执行此操作.

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.

重要的是要记住 page2.php 完全不知道 page1.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.

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

这篇关于如何在 PHP 中的页面之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆