Jump to content

[SOLVED] Sessions


sseeley

Recommended Posts

Hi I am trying to use the following code but the text in the field will not carry accross to the second page, can anyone help?

 

sessionTest1.php

<?php

                session_start();

?>

<html?

<head>

<title>Testing Sessions</title>

</head>

<body>

<?php

$_SESSION['session_var'] = "Testing";

echo "This is a test of the session feature.

<form action='sessionTest2.php' method='POST'>

<input type='text' name='test'>

<input type='submit' value='go'>

</form>";

$_SESSION['session_var1'] = $_POST['test'];

?>

</body>

</html>

 

sessionTest2.php

<?php

                session_start();

?>

<html?

<head>

<title>Testing Sessions 2</title>

</head>

<body>

<?php

if ( @$_SESSION['session_var'] == "Testing" )

{

echo $_SESSION['session_var'];

echo $_SESSION['session_var1'];

}

?>

</body>

</html>

 

 

Many thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/138250-solved-sessions/
Share on other sites

you should use code tags when posting on this site but your setting a session value as a post value in sessionTest1.php when the page hasn't been posted yet. you need to set the session value on sessionTest2.php like this:

 

sessionTest1.php

<html>
<head>
<title>Testing Sessions</title>
</head>
<body>
This is a test of the session feature.
<form action='sessionTest2.php' method='post'>
<input type='text' name='test'>
<input type='submit' name='submit' value='go'>
</form>
</body>
</html>

 

sessionTest2.php

<?php
                session_start();
?>
<html>
<head>
<title>Testing Sessions 2</title>
</head>
<body>
<?php
//get the variables
$test = $_POST['test'];
$submit = $_POST['submit'];

if ($submit) //if the form is submitted
{
$_SESSION['session_var'] = $test;
}

echo $_SESSION['session_var'];
?>
</body>
</html>

 

that should work for you. I am assuming you are just trying to figure out how to use sessions?

Link to comment
https://forums.phpfreaks.com/topic/138250-solved-sessions/#findComment-722798
Share on other sites

Thanks for your help, yes I am trying to learn sessions, just want to pass information from a text field from one page to another...but I get the following error...

 

Parse error: syntax error, unexpected T_STRING, expecting '(' in /home/mypcguy2/public_html/atcsquadron.co.uk/middlesexwing/sports/sessionTest2.php on line 16

Link to comment
https://forums.phpfreaks.com/topic/138250-solved-sessions/#findComment-722804
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.