Forum Moderators: coopster

Message Too Old, No Replies

Session and forms questions.... Newbie

session variables from form

         

aceskull

9:09 am on Apr 6, 2004 (gmt 0)

10+ Year Member



Hi guys, I am like most of the ppl am really new to PHP. What I am trying to do is after getting info from a form if they want to click on a link and come back to see what they picked, it shows it to them. I can't get sessions to work. server doesn't support mysql so only can do it in PHP. basicly, just want the brower to remember what they picked as long as they are on the site.

thanks any help would be great

barn_de

9:20 am on Apr 6, 2004 (gmt 0)

10+ Year Member



hi aceskull,

and welcome to webmasterworld.

uhi, thats a strange problem. do you know why you can't get sessions to work?

do you have

session_start();

in every php file you use? you have to put it at the top. it should be the first thing you call in every php file.

if you really have no opportunity to get sessions to work i can only think of a solution by passing the info to all urls.

but this means, you have to change the urls in the file on the fly.

e.g. normal link to the homepage looks like this "/index.php". now someon adds smth to the cart. now every link on the page looks like this "/index.php?prod[][12]=2" whereas 12 is the product id and 2 is the quantity. if the user selects a second product it would look like this "/index.php?prod[][12]=2&prod[][343]=1". but this gets really ugly the bigger the cart gets :D

hope this helps.

barn

aceskull

9:41 am on Apr 6, 2004 (gmt 0)

10+ Year Member



thanks for the reply barn!

I got sessions to work, now I can't seem to make them remember what the user selected. Once the form gets submitted it echos what they had typed, but once I refresh the page or goto a different page and come back to it, it doesn't remember it anymore. I think its happening becuase I am relying on POST, but I don't know what else to use.. i skip the title tags and html tags just here

here is a test code from the two files
test.php

<body>
<form method="post" action="test2.php">
<input type="text" name="textbox">
<input name="" type="submit" value="enter">
</form>
</body>

code test2.php
<?php session_start();
$_SESSION["check"] = $_POST['textbox'];
?>
<html>

<body>

<?php echo 'what was the session value' . $_SESSION["check"];?>

</body>
</html>

barn_de

9:49 am on Apr 6, 2004 (gmt 0)

10+ Year Member



Hi aceskull,

thats just a small prob. you write the $_SESSION every time. and if you refresh the $_POST array should be empty, because you didn't submit it again. so you wirte an empty value in the session.

so you have to check if there was really a post done.
this should work:

if(count($_POST) >= 1) {
$_SESSION["check"] = $_POST['textbox'];
}

this time it gets only written, if there was a real post.

barn

aceskull

10:04 am on Apr 6, 2004 (gmt 0)

10+ Year Member



thats just it, how do I keep it to remember that post, after the refresh. something like a shoppinig cart. once you used the cart it came to me that most sites that have carts, do that. We add the product to the cart. and a day later we come back it remembers what was in the cart. Thats what I want.

I appreciate all your time and help. I am reading and practicing code like crazy to learn this language, but just am wayyyy to new....

barn_de

11:58 am on Apr 6, 2004 (gmt 0)

10+ Year Member



ok. so you have to store the shopping cart infos also in a cookie.

here you can find a manual:

[de.php.net...]

here some examples you can use:

setcookie ("prod[12]", 2, time()+86400);
setcookie ("prod[364]", 1, time()+86400);

86400 seconds = 1 day. so this cookie will be valid for 1 day.

to get the cookie values use:

if (isset($_COOKIE['prod'])) {
foreach ($_COOKIE['prod'] as $ProdId => $iAmount) {

put_whatever_you_have_to_do_here

}
}

barn