Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,501 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,368 people online right now. Registration is fast and FREE... Join Now!




Sessions in PHP

 
Reply to this topicStart new topic

Sessions in PHP

squiggly
24 Feb, 2007 - 03:09 PM
Post #1

New D.I.C Head
*

Joined: 22 Dec, 2006
Posts: 11



Thanked: 1 times
My Contributions
I have an assignment where i have three html pages for user input and a php confirmation page which needs to display what the user has entered on the previous three pages. Displaying this information in the php page is what i'm having problems with. The page needs to display the quantity, size and colour of widgets the user wants to buy. I have used hidden fields for all three variables and size and colour are displayed fine but i cant get the quantity to be displayed. I think i need to use sessions to solve this but as i am new to php i'm not sure how they work.

Here's my code:
Page 1
CODE

<html>
<head>
<title>Shopping Page</title>
</head>
<body>

<form action = "size.php" method = "post">
Select the quantity of widgets you require
<select name = "selqty">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
</select>

<br/><br/>
<input type = "submit" value = "Buy"/>
</form>
</body>
</html>


Page 2
CODE

<html>
<head>
<title>Select Size page</title>
</head>
<body>


<form action = "selectcolour.php" method = "post">
Select the size of widgets you require
<select name = "selsize">
    <option>Small</option>
    <option>Medium</option>
    <option>Large</option>
    <option>Extra Large</option>
</select>

<br/><br/>

<input type = "hidden" name = "hdqty" value = "<?php echo $_POST[selqty]?>"/>
<input type = "submit" value = "Buy"/>
</form>
</body>
</html>


Page 3
CODE

<html>
<head>
<title>Select colour page</title>
</head>
<body>

<form action = "confirmation1.php" method = "post">
Select the colour for the <?php echo $_POST[selqty]?> widgets you are ordering
<select name = "selcolour">
    <option>White</option>
    <option>Red</option>
    <option>Yellow</option>
    <option>Green</option>
    <option>Blue</option>
</select>
<br/><br/>

<input type = "hidden" name = "hdqty" value = "<?php echo $_POST[selqty]?>"/>
<input type = "hidden" name = "hdsize" value = "<?php echo $_POST[selsize]?>"/>

<input type = "submit" value = "Buy"/>
</form>
</body>
</html>


PHP file:
CODE

<?
    session_start();
    
    
    $_SESSION['quantity'] = "selqty";
    
    echo $_SESSION['quantity'];

    
    echo "<h2>Your size selection is $_POST[hdsize]</h2><br/>";
    echo "<h2>The colour selected is $_POST[selcolour]</h2>";

    
?>


As you can see i've put in a session variable for quantity but all that is displayed is the word 'selqty', rather than the quantity option entered by the user. I don't know how to make it select the correct option from the first page. Can anyone help please?







User is offlineProfile CardPM
+Quote Post

Craige
RE: Sessions In PHP
24 Feb, 2007 - 04:35 PM
Post #2

D.I.C Head
**

Joined: 2 Nov, 2006
Posts: 63


My Contributions
I would recommend saving the $_POST from one page into $_SESSION on the next page. Then, on the final page (4th sniplet), verify the integrity of the $_SESSION values, and do what you wish with them.
User is offlineProfile CardPM
+Quote Post

Spider
RE: Sessions In PHP
24 Feb, 2007 - 05:03 PM
Post #3

Arachnid
****

Joined: 10 Jul, 2002
Posts: 769


My Contributions
You're approach of using hidden fields to store data on long forms is a nice simple one which doesn't require any sessions at all.

You had the right idea with some of your code on the PHP page:
CODE

    echo "<h2>Your size selection is $_POST[hdsize]</h2><br/>";
    echo "<h2>The colour selected is $_POST[selcolour]</h2>";


You just need to follow that through and do the same thing for the other values. After all, they have all simply been passed from page to page in the form in the same way.
User is offlineProfile CardPM
+Quote Post

squiggly
RE: Sessions In PHP
25 Feb, 2007 - 07:24 AM
Post #4

New D.I.C Head
*

Joined: 22 Dec, 2006
Posts: 11



Thanked: 1 times
My Contributions
QUOTE(Spider @ 24 Feb, 2007 - 06:03 PM) *

You're approach of using hidden fields to store data on long forms is a nice simple one which doesn't require any sessions at all.

You had the right idea with some of your code on the PHP page:
CODE

    echo "<h2>Your size selection is $_POST[hdsize]</h2><br/>";
    echo "<h2>The colour selected is $_POST[selcolour]</h2>";


You just need to follow that through and do the same thing for the other values. After all, they have all simply been passed from page to page in the form in the same way.



i have used a hidden field for quantity just like the other two variables but it is still not displayed on the page when i echo it. any ideas?

CODE

<?
    

    echo "<h2>Your order qty is $_POST[hdqty]</h2><br/>";
    echo "<h2>Your size selection is $_POST[hdsize]</h2><br/>";
    echo "<h2>The colour selected is $_POST[selcolour]</h2>";

    
?>

User is offlineProfile CardPM
+Quote Post

snoj
RE: Sessions In PHP
25 Feb, 2007 - 07:27 AM
Post #5

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
http://php.net/manual/en/language.types.ar...pes.array.donts

Also make sure that the form's method is set to post.
User is offlineProfile CardPM
+Quote Post

hienduchuynh
RE: Sessions In PHP
26 Feb, 2007 - 07:42 PM
Post #6

New D.I.C Head
*

Joined: 26 Oct, 2006
Posts: 4


My Contributions
This ex :

page1.php : Register Session :
CODE
<?php
session_start();

$a="abc";

session_register("a");
?>
<html>
<body>
1 session has been registered
<a href="page2.php">Next page</a>
</body>
</html>


page2.php : Use Session

CODE
<?php
session_start();

?>
<html>
<body>
<?
    echo "a : $a";
?>
<a href="page1.php">Previous page</a>
</body>
</html>


[mod edit] use the code tags! They are there for a reason!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month