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.
简单地说,第一页。我有一个由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 $_GET
fields when my form is "post".
在提交给我的邮件中,我收到了“page2”的联系方式。php,但是我从page1没有收到任何信息。,所以变量没有被正确地传递。除了每个页面上的PHP之外,我还在“page2”上使用HTML表单中的隐藏值。“php”以响应在“page1.php”中以HTML形式输入的数据。我知道我的问题之一是,当我的表单是“post”时,我有两个$_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时,计算器就不再工作了。我试图把它与别人建议的不同代码片段放在一起。“所述的形式。php有13个字段,命名为“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"? Sorry the code is such a mess, if anyone could point me in the right direction, that would be great.
那么,在page1中放置正确的PHP是什么呢?php”和“page2.php”?对不起,代码太乱了,如果有人能指出我的正确方向,那就太好了。
5 个解决方案
#1
6
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中的所有值。将它们存储在服务器端状态(会话)或客户端状态(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. You can do this with hidden fields, cookies or sessions as well.
编辑:这是page1的一个例子。发送page1值的php。所以page2 php。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、表单帖子或者get。
<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>
#2
3
You can try using $_REQUEST
instead of $_GET
or $_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 -它对两个都有效。此外,如果在第2页的表单中输入
#3
2
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.
你也可以使用会话,但是如果你问我的话,用它们和表单一起使用有点陈腐。
#4
0
It depends on the method
attribute of your HTML form
how the variables get passed to your PHP script.
这取决于HTML表单的方法属性如何将变量传递给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应该使用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.
页面2上的提交按钮。php(大概)在服务器端确实有副作用,所以它应该使用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的HTML源代码。在填写第一个页面后,查看这些值是否正确传递。
#5
0
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.
很抱歉在这里谈到这个话题,我只是想表达我的看法。
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. even if you do the user to "press next", use dhtml and all in 1 file. need server validation? ajax.
布局和设计都可以使用javascript和CSS实现,这两者都是为了实现这一点。即使用户“按下下一步”,也要使用dhtml和所有文件。需要服务器验证吗?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框架(您可能不应该使用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在这里是最好的。
#1
6
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中的所有值。将它们存储在服务器端状态(会话)或客户端状态(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. You can do this with hidden fields, cookies or sessions as well.
编辑:这是page1的一个例子。发送page1值的php。所以page2 php。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、表单帖子或者get。
<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>
#2
3
You can try using $_REQUEST
instead of $_GET
or $_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 -它对两个都有效。此外,如果在第2页的表单中输入
#3
2
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.
你也可以使用会话,但是如果你问我的话,用它们和表单一起使用有点陈腐。
#4
0
It depends on the method
attribute of your HTML form
how the variables get passed to your PHP script.
这取决于HTML表单的方法属性如何将变量传递给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应该使用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.
页面2上的提交按钮。php(大概)在服务器端确实有副作用,所以它应该使用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的HTML源代码。在填写第一个页面后,查看这些值是否正确传递。
#5
0
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.
很抱歉在这里谈到这个话题,我只是想表达我的看法。
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. even if you do the user to "press next", use dhtml and all in 1 file. need server validation? ajax.
布局和设计都可以使用javascript和CSS实现,这两者都是为了实现这一点。即使用户“按下下一步”,也要使用dhtml和所有文件。需要服务器验证吗?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框架(您可能不应该使用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在这里是最好的。