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

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

问题描述

简单介绍一下page1.php,我有一个由HTML表单组成的计算器,然后PHP代码汇总输入并显示总价格。在价格下方,它还显示page2.php的链接,其中包含一个HTML表单,可在其中输入他们的联系信息。在提交表格后,他们在定价计算器中对page1.php所做的选择以及page2.php上的联系信息通过电子邮件发送给我,并且它们被重定向到主页。



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



然而,当我改变它以至于一切都是 $ _ 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']);
echoYour Price is \ $.number_format($ total,2,'。',',')。 < BR> 中;
echo('>获取您的项目开始< / a>');
}
?>

第二种形式使用隐藏值来回显page1.php中的信息,并且还有三个名为name,email和details的字段。 b
$ b

 <?php 
$ to =jessica@designs.com;
$ message =Pages:\ t $ _POST [one] \\\
;
$ message。=Pages:\ t $ _POST [two] \\\
;
$ message。=Pages:\ t $ _POST [three] \\\
;
$ message。=电子商务:\ t $ _POST [four] \\\
;
$ message。=无电子商务:\ t $ _POST [five] \\\
;
$ message。=CMS:\ t $ _POST [six] \\\
;
$ message。=No CMS:\ t $ _POST [ 7] \\\
;
$ message。=音频或视频:\ t $ _POST [eight] \\\
;
$ message。=Flash Intro:\ t $ _POST [nine] \\\
;
$ message。=图片库:\ t $ _POST [ten] \\\
;
$ message。=平面设计或徽标:\ t $ _POST [11] \\\
;
$ message。=复制: \ t $ _POST [12] \\\
;
$ message。=图片:\ t $ _POST [十三] \\\
;
$ message。=价格总计:\\ \\t $ _POST [总] \\\
;
$ message。=名称:\ t $ _POST [name] \\\
;
$ message。=电子邮件:\ t $ _POST [email] \\\
;
$ message。=\\\
;
$ message。=\\\
;
$ message。=详情:\ t $ _POST [details] \\\
;
邮件($ to,$ subject,$ message,$ headers);
}
?>

那么将正确的PHP放在page1.php和page2.php ?对不起,代码是如此混乱,如果任何人都可以指出我的方向是正确的,那就太好了。

解析方案

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

你需要让page2.php读取page1.php中的所有值并存储它们无论是在服务器端状态(会话)还是客户端状态(cookie或者表单中的隐藏字段)



如果您想要任何这种安全或秘密,那么你也必须考虑所有这些。我解释的任何内容都是秘密或安全的。



编辑:这里是page1.php的一个例子,它发送page1的值。 PHP到page2.php作为获取参数。您可以使用隐藏字段,cookie或会话来做到这一点。



重要的是记住page2.php完全不了解page1.php,不会像编程一样获得价值。每当看到一个完整的网页时,每个页面都会开始和结束,所以您必须使用一些额外的技巧来保留值。这些技巧是会话,cookies,表单帖子或获取。

 < html> 
< head>
< title>页面1< / title>
< / head>
< body>
<?php
//设定默认值,假设最差
$ total = 0;
$ one = 0;
$ two = 0;

//验证我们在$ __ POST
中有这个值if(isset($ _ POST ['submit']))
{
$ submit = $ _POST [ '提交'];
//如果它是真的,尝试一些数学
if($ submit ==sub-total)
{
if(isset($ _ POST ['one']] ))
{
$ one = $ _POST ['one'];
//添加检查以查看您的值是否为数字
if(!is_numeric($ one)){$ one = 0;}
}

if( isset($ _ POST ['two']))
{
$ two = $ _POST ['two'];
if(!is_numeric($ two)){$ two = 0;}
}
$ total = $ one + $ two;
echoYour Price is \ $.number_format($ total,2,'。',',')。 < BR> 中;

if($ submit ==submit)
{
//转到第二页,page1.php中的总数作为$ __ GET值传递
header(Location:page2.php?total =。$ total);
}
}
?>
< form method =postaction =page1.php?submit = true>
< input type =textname =oneid =onevalue =<?= $ one?>/>
< input type =textname =twoid =twovalue =<?= $ two?>/>
< input type =submitname =submitvalue =sub-total/>
< input type =submitname =submitvalue =submit/>
< / form>
< / body>
< / html>


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.

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

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

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:\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.

解决方案

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

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.

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.

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天全站免登陆