--找到解决方案--
正确的 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 的每个项目)。例如,如果客户有 1 个 X,4 个 Y,但 0 个 Z,我希望下一页显示:
1 X
4 是
表格可以在这里看到。出于某种原因,我不断收到“未定义索引”错误。
这是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>
提前致谢。