Sessions and Forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Sessions and Forms

Post by theoph »

The following is my code to use a form field to insert a value into to Session variable. However, I can't get the session variable to be updated when the form action is made. Yes I am a newbie! :roll:

Code: Select all

<?php
	session_start();
	session_register('session_var');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Testing Sessions page 1</title>
</head>

<body>
<?php
	if($form_var) &#123;
		$session_var = $form_var;
	&#125;
?>
<form action="sessionTest2.php" method="post">
	<input name="form_var" type="text" value="testing">
	<input name="" type="submit" value="Go to Next Page">
</form>
</body>
</html>
ody
Forum Contributor
Posts: 147
Joined: Sat Mar 27, 2004 4:42 am
Location: ManchesterUK

Post by ody »

have you got register_globals set to off? If so you need to use the super globals $_SESSION and $_POST to access the varibles:

<?php
if(isset($_POST[form_var])) {
$_SESSION[session_var]= $_POST[form_var];
echo $_SESSION[session_var];
}
?>
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

Tried the code you suggested and it didn't work either. Can't understand what is going to on.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

In sessionTest2.php you'll need ..

session_start();
if(!empty($_POST['form_var'])){
$_SESSION['form_var'] = $_POST['form_var'];
}
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

Thanks it work!
Post Reply