提问人:Maor Polak 提问时间:1/29/2012 最后编辑:Maor Polak 更新时间:2/22/2013 访问量:2146
在 PHP 会话中,如何回显已填写的表单值?
In a PHP session, how do I echo out a form value that has been filled out?
问:
--找到解决方案--
正确的 PHP 代码是 user1175332 和 shanethehat 下面提供的两个答案的组合:
正确的HTML代码:
<?php session_start() ?>
<form method="post" action="session7.php" id="form1">
<input type="text" name="book" value="" id="book"/> Book Box
<input type="submit" value="submit" id="submit" name="submit"/>
</form>
正确的PHP代码:
<?php session_start();
if ($_POST && isset($_POST['book'])) {
$_SESSION['book'] = $_POST['book'];
}
?>
<?php
if (isset($_SESSION['book']) && $_SESSION['book'] > 0)
{echo $_SESSION['book'] . ' Book Box(es)';}
else {echo '';}
?>
这在隔离的测试环境中有效。不幸的是,它仍然不适用于我正在使用的实际形式,可能是因为它包含的 javascript。
----- 原始帖子-----
我有一个表单,用户在其中填写不同项目的数量(X 的 1、Y 的 4 等)提交表格后,他将被带到下一页,其中根据总数量/体积显示价格(这部分工作感谢 StackOverflow 的家伙)。
在下一页上,我尝试显示已填写的每个项目(即数量大于 0 的每个项目)。例如,如果客户有 X 的 1,Y 的 4,但 Z 的 0,我希望下一页显示:
1 x
4 Y
表格可以在这里看到。出于某种原因,我不断收到“未定义索引”错误。
以下是PHP代码:
<?php session_start();
if ($_POST && !empty($_POST['TOTAL'])) {
$_SESSION['TOTAL'] = $_POST['TOTAL'];
}
/* this part is the first problematic part*/
if ($_POST && !empty($_POST['PROD_SP_1.5'])) {
$_SESSION['PROD_SP_1.5'] = $_POST['PROD_SP_1.5'];
}
?>
/* end of the first problematic part*/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>Untitled 1</title>
</head>
<body>
/* this part is the second problematic part*/
<?php
if ($_SESSION['PROD_SP_1.5'] > 0) {
echo $_SESSION['PROD_SP_1.5'] . 'Book Box';
}
else {
echo '';
}
?>
/* end of the second problematic part*/
<!-- This part posts the total volume in Cubic Feet-->
<?php
if (isset($_SESSION['TOTAL'])) {
echo 'Your total volume is ' . round($_SESSION['TOTAL']) . ' Cubic Feet.';
} else {
echo 'You did not fill out any details. Please go back.';
}
?>
<!-- End of volume posting -->
<br/><br/>
<!-- This part posts the correct price based on the total volume -->
<?php
if ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 250) {
echo 'The guaranteed price for door to door service is $1,899.00 based on 1 Section (up to 250CF).';
} elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 500) {
echo 'The guaranteed price for door to door service is $3,349.00 based on 2 Sections (up to 500CF).';
} elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 750) {
echo 'The guaranteed price for door to door service is $4,899.00 based on 3 Sections (up to 750CF).';
} elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 1000) {
echo 'The guaranteed price for door to door service is $5,999.00 based on an exclusive 20ft Container.';
}
elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 1250) {
echo 'The guaranteed price for door to door service is $7,499.00 based on 5 Sections (up to 1,250CF).';
}
elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 1500) {
echo 'The guaranteed price for door to door service is $8,599.00 based on 6 Sections (up to 1,500CF).';
}
elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 1750) {
echo 'The guaranteed price for door to door service is $9,499.00 based on 7 Sections (up to 1,750CF).';
}
elseif ($_SESSION['TOTAL'] > 0 && $_SESSION['TOTAL'] <= 2000) {
echo 'The guaranteed price for door to door service is $9,999.00 based on an exclusive 40ft Container.';
}
else {
echo 'Sorry, your total volume is too high to quote online. Please contact us to set up an on- site survey: 1.877.430.1300';
}
?>
<!-- end of price section -->
</body>
提前致谢。
答:
0赞
shanethehat
1/29/2012
#1
您需要确保已设置会话值,因为页面上方的条件意味着它可能不存在:$_SESSION['PROD_SP_1.5']
/* this part is the second problematic part*/
<?php
if (isset($_SESSION['PROD_SP_1.5']) && $_SESSION['PROD_SP_1.5'] > 0) {
echo $_SESSION['PROD_SP_1.5'] . 'Book Box';
}
您还应该使用而不是在检查 id 时 POST 数组中是否存在值:isset
empty
if ($_POST && isset($_POST['PROD_SP_1.5'])) {
评论
0赞
Maor Polak
1/29/2012
谢谢你的建议shanethehat。我更新了代码,但仍然没有输出。似乎 php 代码无法识别何时在表单上填写了数量。
0赞
Maor Polak
1/29/2012
谢谢 shanethehat 的更新 - 仍然没有运气。我会咨询我办公室的PHP程序员,看看他能不能帮忙。如果可以的话,他会在这里更新帖子。
1赞
user1175332
1/29/2012
#2
- 您是否在每一页上都包含?如果要使用会话,这是必需的
<?php session_start();?>
- 我看到你只在它不是空时设置。如果它为空,我建议将其设置为 0,或将测试更改为
$_SESSION['PROD_SP_1.5']
if (isset($_SESSION['PROD_SP_1.5']) && $_SESSION['PROD_SP_1.5'] > 0)
评论
0赞
Maor Polak
1/29/2012
您好,是的,我已经包含了<?php session_start() ;?>所有页面上。我尝试了您和 shanethehat 的建议,但问题仍然存在。此代码不提供任何输出
0赞
user1175332
1/29/2012
也许我误会了。你只用了两页吗?一个用于表单,另一个用于显示结果?如果是这样,则可以在打印结果时使用 _POST 美元而不是 _SESSION 美元
0赞
Maor Polak
1/29/2012
感谢您的更新 - 我尝试了 _POST 美元,但仍然没有运气。表单根据用户在表单上所做的选择将用户重定向到其他页面,但本质上是的,只有两个页面(1 是表单页面,另一个是用户被定向到的页面之一)。
评论
isset()