Page 1 of 1

Sessions and Forms

Posted: Sun Mar 28, 2004 2:21 pm
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>

Posted: Sun Mar 28, 2004 2:31 pm
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];
}
?>

Posted: Sun Mar 28, 2004 3:16 pm
by theoph
Tried the code you suggested and it didn't work either. Can't understand what is going to on.

Posted: Sun Mar 28, 2004 3:20 pm
by markl999
In sessionTest2.php you'll need ..

session_start();
if(!empty($_POST['form_var'])){
$_SESSION['form_var'] = $_POST['form_var'];
}

Posted: Sun Mar 28, 2004 10:15 pm
by theoph
Thanks it work!