在PHP会话中,如何回显已填写的表单值?

时间:2012-01-28 16:24:35

标签: php forms session if-statement undefined-index

- 找到解决方案 -

正确的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 Y

可以看到表格here。出于某种原因,我不断收到“Undefined Index”错误。

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

提前致谢。

2 个答案:

答案 0 :(得分:1)

  1. 您是否在每个页面都包含<?php session_start();?>?如果您想使用会话,则需要这样做
  2. 我发现如果它不是空的话,你只设置$_SESSION['PROD_SP_1.5']。我建议如果它为空,则将其设置为0,或将测试更改为if (isset($_SESSION['PROD_SP_1.5']) && $_SESSION['PROD_SP_1.5'] > 0)

答案 1 :(得分:0)

您需要确保已设置会话值,因为页面上方的条件意味着$_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数组时,您还应该使用isset而不是empty

if ($_POST && isset($_POST['PROD_SP_1.5'])) {