Jump to content

Recommended Posts

Hi,

 

As I mentioned in my intro, I've just started learning PHP. I'm trying to write a script which will have three visible pages - one is asking for the user's name, and which page they would like to visit. The other two will be the pages which they can select from the first page, and a message saying hello.

 

I've got this working through the session tag, but only when the form code is set to go a particular page. To allow the user to select, I've added a non-visible page, called redirect.php, which takes the value selected in the first page (the drop-down, for which page they'd like to visit), and sends them to that page. When I go through the redirect page, the session doesn't appear to 'keep' the user's name, entered on the first page.

 

The redirect script seems to work okay, but the variable containing the username seems to be blank in the final script.

 

Can someone advise where I'm going wrong? The three scripts are below:

 

Starting Page:

<?php
session_start();
$_SESSION[‘uname’] = “testing”;
?>
<html>
<head>
</head>
<body>
<?php
echo "<form action='redirect.php' method='post'>
<p><input type='text' name='uname'><br>
<select name='gourl'>
<option value=''>Choose a Destination...
<option value='test2.php'>Test Page 2
<option value='test3.php'>Test Page 3
</select>
<input type='submit' value='Go'>
</form>";
?>
</body></html>

 

Redirect Script:

<?php
$url = $_POST["gourl"];
header("Location: $url");
?> 

 

Final Page:

<html><body>
<?php
$testinghere = $_SESSION['uname'];
echo "Hello, $testinghere \n";
?>
</body></html>

 

I've tried loads of small edits, but like I said, I'm a beginner, so any expert advice would be very much appreciated!

Link to comment
https://forums.phpfreaks.com/topic/208826-help-with-forms-and-session/
Share on other sites

you need to put session_start() into your final page also, before <html> as well as it send headers if you have session.use_cookies enabled i believe

 

Hey,

 

Thank you for your help.

 

I've just tried adding the session_start line, but still the variable value is not shown on the final page.

 

Can you see anything else wrong?

Here's a tested solution:

 

a.php:

 

<?php
session_start();

if (isset($_POST['submit']))
{
$_SESSION['uname'] = $_POST["uname"];
$url = $_POST["gourl"];
header("Location: $url");
}

?>
<html>
<head>
</head>
<body>

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<p><input type='text' name='uname'><br>
<select name='gourl'>
<option value=''>Choose a Destination...</option>
<option value='b.php'>Page B</option>
</select>
<input type='submit' value='Go' name='submit'>
</form>

</body></html>

 

b.php:

<?php
session_start();
?>

<html><body>
<?php
$testinghere = $_SESSION['uname'];
echo "Hello, $testinghere \n";
?>
</body></html>

 

You'll need to input a value into the uname field to see the session echo on the next page.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.