The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> sessions trouble
Mindapolis
post Nov 9 2011, 06:54 PM
Post #1


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



I'm having problems with sessions. if I post the code could someone please help me?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
2 Pages V  1 2 >  
Reply to this topicStart new topic
Replies(1 - 19)
Darin McGrew
post Nov 10 2011, 02:10 AM
Post #2


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



What kinds of "problems with sessions"?

You're better off learning how to debug your own programs. We can help with that. But it's unlikely that we'll be able to find the problem just by looking at your code.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Frederiek
post Nov 10 2011, 02:35 AM
Post #3


Programming Fanatic
********

Group: Members
Posts: 5,146
Joined: 23-August 06
From: Europe
Member No.: 9



These might help:
http://devzone.zend.com/node/view/id/627
http://www.php.net/manual/en/book.session.php
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Mindapolis
post Nov 10 2011, 09:57 AM
Post #4


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



Hi, first of all I'm really not trying to get anyone to do my work. I have read many articles on sessions but it 's just not clicking how to transfer information from onepage to another. For example, I'm working on a website that I need to be able to be able to transfer the product name and quantity from the product page to the checkout page. On the products page if I click "order treats" it will go to the checkout page but it will not transfer the quantity. I can display the product page code but the checkout page code is long so I will display only necessary code.

CODE


<?php
session_start();
if(isset($_POST['empty']) || !isset($_SESSION['cart']))
    $_SESSION['cart'] = array();  //if a cart is clear or doesn't exist yet
if(isset($_POST['quantity'])  && isset($_POST['product_title']))
    {
        $product_title = $_POST['product_title'];
    $quantity = $_POST['quantity'];

$cart = $_SESSION['cart'];
if( isset($cart[$product_title]) ){
        // If so, add quantity to it.
        $cart[$product_title] += $quantity;
$_SESSION['cart']=$cart;// ADDED LINE
}
    else{
        // Otherwise, set it to the quantity
        $cart[$product_title] = $quantity;
$_SESSION['cart']=$cart;// ADDED LINE

    // Destroy the reference to $cart to avoid acidentally screwing with it elsewhere in the script
    unset( $cart );
}
}        
require_once("functions.php");
DatabaseConnection();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
td {
    border-top-style: solid;
    border-right-style: solid;
    border-bottom-style: solid;
    border-left-style: solid;
    border-top-color: #30C;
    border-right-color: #30C;
    border-bottom-color: #30C;
    border-left-color: #30C;
}
#productCatalog {
    width:400px;  
    margin-right: auto;
    margin-left: auto;
}
</style>
<link href="doggyTreats.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
logo();
navBar();
echo "<div id=\"productCatalog\">";
echo "<form action=\"checkOut.php\" method=\"post\" name=\"catalog\">";
  
DatabaseConnection();  

  $query = "SELECT * FROM treats";
        $result_set = mysql_query($query) or die(mysql_error());
$i = 0;

        echo "<table>";
        while ($row = mysql_fetch_array($result_set))
        {
        echo"<tr><td width=\"2s00px\"><img src=\"{$row['product_pic']}\" /></td><td width=\"200px\">{$row['product_title']}.<br /><br />{$row['product_Description']}.<br /> Price:  \${$row['price']}{$row['pricePer
        ']}.<br /><br />Quantity <input name=\"quantity\" type=\"text\" size=\"2\" /></td></tr>";
        }
        echo "<tr>";
            echo "<td><input name=\"submit\" type=\"submit\" value=\"Proceed to Checkout\" />";
        echo "</table>";    
        echo "</form>";
echo "</div>";
footer();
?>
</body>
</html>'


[code]
<?php
session_start();
if(isset($_POST['empty']) || !isset($_SESSION['cart']))
$_SESSION['cart'] = array(); //if a cart is clear or doesn't exist yet
if(isset($_POST['quantity']) && isset($_POST['product_title']))
{
$product_title = $_POST['product_title'];
$quantity = $_POST['quantity'];

$cart = $_SESSION['cart'];
if( isset($cart[$product_title]) ){
// If so, add quantity to it.
$cart[$product_title] += $quantity;
$_SESSION['cart']=$cart;// ADDED LINE
}
else{
// Otherwise, set it to the quantity
$cart[$product_title] = $quantity;
$_SESSION['cart']=$cart;// ADDED LINE

// Destroy the reference to $cart to avoid acidentally screwing with it elsewhere in the script
unset( $cart );
}
}
require_once("functions.php");
DatabaseConnection();
?>

html code
<tr>
<ul>
<?php
foreach($_SESSION['cart'] as $product_title => $quantity)
{
echo "<li>$product_title $quantity</li>";
}
print_r($_SESSION['cart']);
?>
</ul>
</tr>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Nov 10 2011, 12:06 PM
Post #5


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



In a sense, the point of sessions is that you don't have to transfer information from one page to another. Instead, you transfer information to the server. That information is stored on the server, with the session ID as the key. Later, when another page needs that information, it uses the session ID to look it up.

So, how does the "order treats" link/button/whatever transfer information to the server? How does the server associate that information with the session ID? Have you tested the software to see whether that is happening correctly?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Mindapolis
post Nov 10 2011, 12:24 PM
Post #6


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256




I'm sorry, but could you rephrase these questions?
So, how does the "order treats" link/button/whatever transfer information to the server? How does the server associate that information with the session ID?


QUOTE(Darin McGrew @ Nov 10 2011, 12:06 PM) *

In a sense, the point of sessions is that you don't have to transfer information from one page to another. Instead, you transfer information to the server. That information is stored on the server, with the session ID as the key. Later, when another page needs that information, it uses the session ID to look it up.

So, how does the "order treats" link/button/whatever transfer information to the server? How does the server associate that information with the session ID? Have you tested the software to see whether that is happening correctly?

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Nov 10 2011, 02:27 PM
Post #7


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



When the user clicks on the "order treats" link/button/whatever, what happens on the server? What do your server-side programs do?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Mindapolis
post Nov 10 2011, 02:41 PM
Post #8


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



it takes them to the checkout page and displays array() under the navbar


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Nov 10 2011, 04:08 PM
Post #9


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



When do you update the session data to include the treats the user ordered?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Mindapolis
post Nov 14 2011, 11:09 AM
Post #10


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



I'm sorry I'm not understanding your question; however, over the weekend I was able to understand how to transfer one form element from one page to about another, but I can't understand how to transfer a set of variables from one page to another. I know I need need to use a foreach loop, but its not working. here 's what I have

product.php
CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$errors=array();
if(isset($_POST['submit']))
{
    if(empty($_POST['name'])){
    $errors['name']=" put your name";
    }
}
?>    
<form action="checkOut.php" method="post" name="test">  
<?php if(isset($errors['name'])){ echo $errors['name']."<br />"; } ?>
Name:  <input name="name" type="text" size="10" maxlength="15" value="<?php if(isset($_POST['name'])){ echo "$_POST[name]"; } ?>" /><br />  
quantity <input name="quantity" type="text" size="2" /><br />  
quantity <input name="quantity" type="text" size="2" /><br />  
<input name="submit" type="submit" value="Submit" />
</form>
</body>
</html>


checkout.php
CODE

<?php
session_start();
$_SESSION['name']=$_POST['name']; //gets users input
$_SESSION['quantity']=$_POST['quantity'];
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table>
<tr>
<?php
$order=array($quantity);
    foreach($order as $value){
    echo $value;
}
?>
</tr>
</table>
</body>
</html>


This post has been edited by Mindapolis: Nov 14 2011, 11:31 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Nov 14 2011, 12:22 PM
Post #11


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



What do you mean by "transfer a set of variables from one page to another"? How is that different from "transfer one form element from one page to about another"?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Nov 14 2011, 01:28 PM
Post #12


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE
its not working. here 's what I have


Do you mean "This is my attempt to write the program myself"?? In which case I suggest the first thing to do is to study how to lay a program out. Currently it's unreadable, because the php angle brackets, html syntax etc is all jumbled up.

I don't understand quite what you mean by "transferring variables". Esssentially you can't do such a thing, except by using forms. A form transfers values for user input to the "action" page it is sent to.

Looking at the 'checkout' page; I don't see what the session stuff is for. Perhaps you should try to write a simple page including a form, and nothing else, which transfers user input to a second page that prints it out.

In particular there are many mysteries here... what is $order=array($quantity); for? As far as I can see $quantity is an undefined variable at this point, so this will probably make $order into an empty array. But I have no idea quite what you are trying to do.


CODE

<table>
<tr>
<?php
$order=array($quantity);
    foreach($order as $value){
    echo $value;
}
?>
</tr>


Notice also that if the bit of php outputs 'XXX' you will get

CODE

<table>
<tr>
XXX
</tr>


which is invalid html. <tr> must include at least one <td>.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Mindapolis
post Nov 14 2011, 01:41 PM
Post #13


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



Brian, why do you have the need to make belittling comments to someone who is already feeling stupid about this topic? Please stop it
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Mindapolis
post Nov 14 2011, 02:16 PM
Post #14


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



Take the code for example. I know if you want to transfer the name over to the checkout page you would put
CODE
  echo $_SESSION['name'];
. I tried putting
CODE
$_SESSION['quantity'];
but the quantity wouldn't transfer. Besides if there is more than one quantity don't I want to use a foreach loop?


QUOTE(Darin McGrew @ Nov 14 2011, 12:22 PM) *

What do you mean by "transfer a set of variables from one page to another"? How is that different from "transfer one form element from one page to about another"?


CODE

<?php
session_start();
$_SESSION['name']=$_POST['name']; //gets users input
$_SESSION['quantity']=$_POST['quantity'];
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table>
<tr>
<?php  echo $_SESSION['name'];
}
?>
</tr>
</table>
</body>
</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Nov 14 2011, 08:05 PM
Post #15


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



Here's a VERY basic tutorial.

http://www.tizag.com/phpT/phpsessions.php

A good place to start.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Nov 14 2011, 11:42 PM
Post #16


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Mindapolis @ Nov 15 2011, 03:41 AM) *

Brian, why do you have the need to make belittling comments to someone who is already feeling stupid about this topic? Please stop it


I'm not making "belittling" comments, unless you think I shouldn't say "jumbled up", but rather "less than optimally laid out".

I see no reason to feel stupid. I think the real problem is that you have not realised at all that understanding programming is quite a big (understatement) job. To a certain extent you can manage to produce (e.g.) html documents by a sort of incremental guess-and-learn process, but for programming I think you really need to go off an study some elementary primers on "what is a program".

But because you refuse to answer questions like "Do you understand XYZ?" or "What do you understand PQW to mean?" any help people try to offer is totally hit-and-miss.

Anyway, that's trying to be helpful.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Nov 14 2011, 11:49 PM
Post #17


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Mindapolis @ Nov 15 2011, 04:16 AM) *

Take the code for example. I know if you want to transfer the name over to the checkout page you would put

CODE
  echo $_SESSION['name'];


I tried putting
CODE
$_SESSION['quantity'];


but the quantity wouldn't transfer. Besides if there is more than one quantity don't I want to use a foreach loop?


No, writing echo $_SESSION['name']; does not transfer anything anywhere. It writes the value of this array element to the current output.

Look, I do not think you have understood sessions *at all*. You do not need to use sessions for simple form handling. I suggest you write the following as a starter:

A page including a form with one input field (text) and a submit button.
A php script called by this form which prints the value of the input field.

If you do this successfully it should mean you have understood the basics of forms. If you need specific help, ask. Until you have done this you will get nowhere trying to use sessions, which are rather more complicated (and in general not essential at all).

This post has been edited by Brian Chandler: Nov 14 2011, 11:50 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Mindapolis
post Nov 15 2011, 09:18 AM
Post #18


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



I'm sorry but you are wrong. using echo $_SESSION['name']; did transfer the name from one page to another.

The code I displayed is on a small scale. What I'm having trouble understanding is how to transfer multiple quantities.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Ephraim F. Moya
post Nov 15 2011, 10:07 AM
Post #19


Advanced Member
****

Group: Members
Posts: 167
Joined: 2-September 07
From: New Mexico
Member No.: 3,702



QUOTE(Mindapolis @ Nov 15 2011, 07:18 AM) *

I'm sorry but you are wrong. using echo $_SESSION['name']; did transfer the name from one page to another.

The code I displayed is on a small scale. What I'm having trouble understanding is how to transfer multiple quantities.


In the top of each program that uses sessions do:

session_start();

This prepares the program to use SESSIONS by getting and sending a cookie containing a pointer into the local SESSION database to the visitor.

During the program add enough $_SESSIONS['xxx']s to carry ALL the data you want for the next time this visitor from this particular domain requests a page.

When you fall off the bottom of the program ALL the data that is in the SESSIONS array is stored locally and keyed to the cookie sent above.

in the meantime many or none other visitors come to get pages from you using a different or no cookie. This particular SESSION array is NOT accessed for them. Their own SESSION array (or none) is accessed.

Finally a visitor with the right cookie shows up and based on that cookie the local SESSION database is read and the SESSION array is filled in.

So, if a SESSION entry contains one value or MANY values doesn't matter.

There are rules about what a SESSION entry can contain, though. You need to learn them. Read the SESSION stuff on php.net over and over until you understand it well.

Brian is right. You're trying to run before you know how to crawl. Start with basic php, go to php with classes, then go to SESSIONS.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Mindapolis
post Nov 15 2011, 01:09 PM
Post #20


Member
***

Group: Members
Posts: 67
Joined: 2-April 11
Member No.: 14,256



I understand about crawling before running and that 's what I'm doing. I know how to transfer one variable to multiple pages but I dont understand how to transfer arrays from one page to another. I found this example on http://www.phpriot.com/articles/intro-php-sessions/7 I understand everything except what elements to put in the array since the elements are the user 's input here 's what I have so far.

when I run this I get

Warning: Invalid argument supplied for foreach() in /home/content/a/u/n/auntievics/html/test/checkOut.php on line 18

CODE

<?php
session_start();
$quantity_desired=array($quantity);
$_SESSION['quantity']=$quantity_desired;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$errors=array();
if(isset($_POST['submit']))
{
    if(empty($_POST['name'])){
    $errors['name']="dipshit, put your name";
    }
}
?>    
<form action="checkOut.php" method="post" name="test">  
<?php if(isset($errors['name'])){ echo $errors['name']."<br />"; } ?>
Name:  <input name="name" type="text" size="10" maxlength="15" value="<?php if(isset($_POST['name'])){ echo "$_POST[name]"; } ?>" /><br />  
quantity <input name="quantity" type="text" size="2" /><br />  
quantity <input name="quantity" type="text" size="2" /><br />  
<input name="submit" type="submit" value="Submit" />
</form>
</body>
</html>


CODE

<?php
session_start();

//$_SESSION['name']=$_POST['name']; //gets users input
//$_SESSION['quantity']=$_POST['quantity'];
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table>
<tr>
<td>  
<?php
foreach($_SESSION['quantity'] as $key=>$value)
//  echo $_SESSION['name'];
  echo $_SESSION['quantity'];  
?>
</td>
</tr>
</table>
</body>
</html>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th March 2024 - 06:53 AM