Maor Polak
Maor Polak

Reputation: 25

In a PHP session, how do I echo out a form value that has been filled out?

--Solution found--

The correct PHP code is a combination of the two answers provided below by user1175332 and shanethehat:

Correct HTML Code:

<?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>

Correct PHP Code:

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


?>

This works in an isolated test environment. Unfortunately it still doesn't work with the actual form I'm using, probably because of the javascript it contains.

----- Original Post -----

I have a form in which the user fills out a quantity for different items (1 of X, 4 of Y, etc.) Once the form is submitted, he is taken to the next page, where a price is shown based on the total quantity / volume (this part works thanks to the guys here at StackOverflow).

On the next page, I am trying to have every item that has been filled out (i.e. every item with a quantity greater than 0) be displayed. For instance, if the customer has 1 of X, 4 of Y, but 0 of Z, I want the next page to show:

1 X
4 Y

The form can be seen here. For some reason, I keep getting an "Undefined Index" error.

Here is the PHP code:

<?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>

Thanks in advance.

Upvotes: 0

Views: 2161

Answers (2)

shanethehat
shanethehat

Reputation: 15570

You need to ensure that the session value has been set, because the condition further up the page means that it is possible for $_SESSION['PROD_SP_1.5'] to not exist:

/* 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';
}

You should also use isset rather than empty when checking id a value exists in the POST array:

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

Upvotes: 0

user1175332
user1175332

Reputation: 58

  1. Did you include <?php session_start();?> on every page? This is required if you want to use sessions
  2. I see that you only set $_SESSION['PROD_SP_1.5'] if it's not empty. I suggest setting it to 0 if it is empty, or changing the test to if (isset($_SESSION['PROD_SP_1.5']) && $_SESSION['PROD_SP_1.5'] > 0)

Upvotes: 1

Related Questions