I am not able to get my email input even after defining my sessions, please look below:
I have two scripts: The first is is to show the declaration of the session, while the second is to display the session's value:
First Script:sessionTest1.php

<?php
session_start();
?>
<html>
<head><title>Testing Sessions page 1</title></head>
<body>
<?php
$_SESSION['session_var'] = "testing";
@$_SESSION['session_var2'] = $_POST['email'];
echo "This is a test of the sessions feature.
<form action='sessionTest2.php' method='POST'>
<input type='hidden' name='form_var'
value='testing'>
<input type='text' name='email'>
<input type='submit' value='go to next page'>
</form>";
?>
</body></html>

Second Script:sessionTest2.php

<?php
session_start();
?>
<html>
<head><title>Testing Sessions page 2</title></head>
<body>
<?php
echo "session_var = {$_SESSION['session_var']}<br>\n";
echo "session_var2 = {$_SESSION['session_var2']}<br>\n";
echo "form_var = {$_POST['form_var']}<br>\n";
?>
</body></html>

Please what could be possibly wrong? or what should i do to obtain the entered value for the email and then be able to pass it along to as many pages as possible?

Recommended Answers

What is the ouput of these lines?

echo "session_var = {$_SESSION['session_var']}<br>\n";
echo "session_var2 = {$_SESSION['session_var2']}<br>\n";

It's better practice to have the variables outside the quotes like this (although this shouldn't affect the output):

echo "session_var = {".$_SESSION['session_var']."}<br>\n";
echo "session_var2 = {".$_SESSION['session_var2']."}<br>\n";
Jump to Post

All 3 Replies

What is the ouput of these lines?

echo "session_var = {$_SESSION['session_var']}<br>\n";
echo "session_var2 = {$_SESSION['session_var2']}<br>\n";

It's better practice to have the variables outside the quotes like this (although this shouldn't affect the output):

echo "session_var = {".$_SESSION['session_var']."}<br>\n";
echo "session_var2 = {".$_SESSION['session_var2']."}<br>\n";

although this shouldn't affect the output

But in this case it would, using echo "Hi {$name}"; is okay, but array variables like $variable['XYZ'] it could well cause problems.

I'm sure the output will tell us where this problem lies.

I solved the problem by adjusting my first script to be like this:

<?php
session_start();
?>
<html>
<head><title>Testing Sessions page 1</title></head>
<body>
<?php
$_SESSION['session_var'] = "testing";
echo "This is a test of the sessions feature.
<form action='sessionTest2.php' method='POST'>
<input type='hidden' name='form_var'
value='testing'>
<input type='text' name='email'>
<input type='submit' value='go to next page'>
</form>";
?>
</body></html>

And my second script to become something like this:

<?php
session_start();
$_SESSION['session_var2'] = $_POST['email']; // This line is needed here, but unneeded in sessionTest1.php
?>
<html>
<head><title>Testing Sessions page 2</title></head>
<body>
<?php
echo "session_var = {$_SESSION['session_var']}<br>\n";
echo "session_var2 = {$_SESSION['session_var2']}<br>\n";
echo "form_var = {$_POST['form_var']}<br>\n";
?>
</body></html>

With the afore mentioned adjustments I was able to get my script to hold the submitted email as one move to other pages till the session is destroyed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.