Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

problems with php sessions

$
0
0

Hi,

 

I am new to php and am having problems with sessions, basically I have a form that I have split into 2 pages. I use session variables to collect the input from both pages and then output the values to a final page that will eventually email the submissions to me. This part works fine, I get all the desired information as required.

 

The problem I am having is that I am trying to make each page retain values as well as using session variables, so if a user decides to go back to make changes, they only have to change the desired field rather than having to re-enter values in all the fields.

 

When I try to go back, all the fields are empty.

 

I have tried using header instead of meta refresh and it still does the same thing. The only way I can get it to work is if I move

 

<?php
session_start();
$_SESSION['clientname'] = $_POST['name']; //name
$_SESSION['clientcompany'] = $_POST['company']; //company
?>

 

and

 

<?php
session_start();
$_SESSION['productname'] = $_POST['product']; //name
$_SESSION['sizename'] = $_POST['size']; //company
?>

 

from the top of the script to next block of php code. The the problem is that everything works but I have to reload the page before I get the session variables to populate.

 

here is the code :

<?php
session_start();
$_SESSION['clientname'] = $_POST['name']; //name
$_SESSION['clientcompany'] = $_POST['company']; //company
?>
<html>

<head>
<title>Page 1</title>

</head>

<body>

<?php
// move session stuff to here, works fine but reload of page needed.


$N = '';
$C = '';


if(isset($_POST['submit']))
  {
  //process the text-input field
  if(isset($_POST['name']))
    $N = $_POST['name'];
  else
    $N = false;
    
  if(isset($_POST['submit']))
  

 
  
  
if(isset($_POST['submit']))
  
  //process the text-input field
  if(isset($_POST['company']))
    $C = $_POST['company'];
  else
    $C = false;




    
  //if all entries okay, process information, display message and exit script.

if ($N && $C)
    {


echo '<META HTTP-EQUIV="Refresh" Content="0; URL=size.php">';    
    //exit;
    
    echo '</body></html>';
    exit();
    }
  }



echo '<h2>Page 1</h2>';
echo '<form name="Form1" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';

//display text-input label for name
if(isset($_POST['submit']) && !$N)
  echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
else
  echo '<p>Please enter Name [required]<br />';
  
//display the text-input field
echo '<input type="text" name="name" maxlength="60" size="60"
               value="' . $_SESSION['clientname'] . '" /></p>';


if(isset($_POST['submit']) && !$C)
  echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
else
  echo '<p>Please enter Company [required]<br />';
  
//display the text-input label for company
echo '<input type="text" name="company" maxlength="60" size="60"
               value="' . $_SESSION['clientcompany'] . '" /></p>';



echo '<p><input type="submit" name="submit" value="submit"></p>';
echo '</form>';

echo '</body>';

echo '</html>';

and size.php
 :

<?php
session_start();
$_SESSION['productname'] = $_POST['product']; //name
$_SESSION['sizename'] = $_POST['size']; //company
?>
<html>

<head>
<title>Page 2</title>

</head>

<body>

<?php
// move session stuff to here, works fine but reload of page needed.

$P = '';
$C = '';


if(isset($_POST['submit']))
  {
  //process the text-input field
  if(isset($_POST['product']))
    $P = $_POST['product'];
  else
    $P = false;
    
  if(isset($_POST['submit']))
  

 
  
  
if(isset($_POST['submit']))
  
  //process the text-input field
  if(isset($_POST['size']))
    $SI = $_POST['size'];
  else
    $SI = false;




    
  //if all entries okay, process information, display message and exit script.

if ($P && $SI)
    {


echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';   

    //exit;
    
    echo '</body></html>';
    exit();
    }
  }


echo '<h2>Page 2</h2>';
echo '<form name="Form1" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';

//display text-input label for name
if(isset($_POST['submit']) && !$P)
  echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
else
  echo '<p>Please enter Product Name [required]<br />';
  
//display the text-input field
echo '<input type="text" name="product" maxlength="60" size="60"
               value="' . $_SESSION['productname'] . '" /></p>';


if(isset($_POST['submit']) && !$SI)
  echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
else
  echo '<p>Please enter Size [required]<br />';
  
//display the text-input label for company
echo '<input type="text" name="size" maxlength="60" size="60"
               value="' . $_SESSION['sizename'] . '" /></p>';



echo '<p><input type="submit" name="submit" value="submit"></p>';
echo '</form>';

echo '</body>';

echo '</html>';

and success.php :

<?php
session_start();
echo 'product type is : ';
    echo $_SESSION['productname'];
    echo "<br>";	
    echo 'Client Name : ';
    echo  $_SESSION['clientname'];
    echo "<br>";
    echo 'Client Company : ';
    echo  $_SESSION['clientcompany'];
    echo "<br>";

session_end();
?>

sorry about the size of the scripts, I have reduced them as much as I can. Any help would be greatly appreciated. Thank you.


Viewing all articles
Browse latest Browse all 13200

Trending Articles