I have four forms

one --> two ---> threee -----> four

I used session here...
when i go from one --- > the code works good
then when i go from two ----> three also works good..
but in the third page, when i click back <----- and then from page 2 when i click back <----- i cant see the data that i typed in form 1...

Can someone help me where i have gone wrong ?

My codes are below:

one.php

<?php
session_start();
$_SESSION['name'];
$_SESSION['phone'] = $_POST['txt_phone'];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>one</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="two.php" >
  <label>
  Name:
  <input type="text" name="txt_name" id="txt_name" value="<?php echo $_SESSION['name']; ?>" />
  </label>
  <p>
    <label>
    <input type="submit" name="submit" id="button" value="Next" />
    </label>
  </p>
</form>
<p>&nbsp;</p>
</body>
</html>

two.php

<?php
session_start();
$_SESSION['name'] = $_POST['txt_name']; 
$_SESSION['email'] = $_POST['txt_email'];
echo $_SESSION['name'];
echo "<br>";
echo $_SESSION['email']; 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Two</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
  <label>Phone Number:
  <input type="text" name="txt_phone" id="txt_phone" value="<?php echo $_SESSION['phone']; ?>" />
  </label>
  <p>
    <label></label>
  </p>
  <table width="360" border="0">
    <tr>
      <th scope="row"><input type="submit" name="button2" id="button2" value="Back" onclick="form1.action='one.php';" /></th>
      <td><input type="submit" name="button" id="button" value="Next" onclick="form1.action='three.php';" /></td>
    </tr>
  </table>
  </form>
</body>
</html>

three.php

<?php
session_start();
$_SESSION['name'];
$_SESSION['phone'] = $_POST['txt_phone'];
$_SESSION['com'] = $_POST['txt_com'];
echo $_SESSION['name'];
echo "<br>";
echo $_SESSION['phone'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>three</title>
</head>

<body> 
<form id="form1" name="form1" method="post" action="">
  <label>Email: 
  <input type="text" name="txt_email" id="txt_email" value="<?php echo $_SESSION['email']; ?>" />
  </label>
  <p>
    <label></label>
  </p>
  <table width="360" border="0">
    <tr>
      <th scope="row"><input type="submit" name="button2" id="button2" value="Back" onclick="form1.action='two.php';" /></th>
      <td><input type="submit" name="button" id="button" value="next" onclick="form1.action='four.php';" /></td>
    </tr>
  </table>
</form>
</body>
</html>

Four.php

<?php
session_start();
$_SESSION['email'] = $_POST['txt_email'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Four</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>Comments:
  <input type="text" name="txt_com" id="txt_com" value="<?php echo $_SESSION['com']; ?>" />
  </label>
  <p>
    <label></label>
  </p>
  <table width="360" border="0">
    <tr>
      <th scope="row"><input type="submit" name="button2" id="button2" value="Back" onclick="form1.action='three.php';" /></th>
      <td><input type="submit" name="button" id="button" value="Submit" onclick="form1.action='result.php';" /></td>
    </tr>
  </table>
</form>
</body>
</html>

UR help is much appreciated..

Where you use $_SESSION[blah]=$_POST you should place that in an if statement like the following.

if (isset($_POST) && !empty($_POST)) {
$_SESSION['blah']=$_POST['blah'];
$_SESSION['asdf']=$_POST['asdf'];
}

This way if $_POST is empty, session will not be assigned an empty value.

Where you use $_SESSION[blah]=$_POST you should place that in an if statement like the following.

if (isset($_POST) && !empty($_POST)) {
$_SESSION['blah']=$_POST['blah'];
$_SESSION['asdf']=$_POST['asdf'];
}

This way if $_POST is empty, session will not be assigned an empty value.

Hey cwarn, thanks for ur help,

But its still the same....
:(

y ?

You can replace the code in the first few lines of every page withing <?php...?> with the following set of codes

one.php

<?php
session_start();

if (!isset($_SESSION['phone']) && isset($_POST['txt_phone'])) {
	$_SESSION['phone'] = $_POST['txt_phone'];
}

?>

two.php

<?php
session_start();
if (!isset($_SESSION['name']) && isset($_POST['txt_name'])) {
	$_SESSION['name'] = $_POST['txt_name'];
}
if (!isset($_SESSION['email']) && isset($_POST['txt_email'])) {
	$_SESSION['email'] = $_POST['txt_email'];
}

?>

three.php

<?php
session_start();

if (!isset($_SESSION['phone']) && isset($_POST['txt_phone'])) {
	$_SESSION['phone'] = $_POST['txt_phone'];
}
if (!isset($_SESSION['com']) && isset($_POST['txt_com'])) {
	$_SESSION['com'] = $_POST['txt_com'];
}

?>

four.php

<?php
session_start();
if (!isset($_SESSION['email']) && isset($_POST['txt_email'])) {
	$_SESSION['email'] = $_POST['txt_email'];
}

?>

Hope this thing helps.

Hi bhanuprakash,
Thanks for the help

Ur Code helps me...

Thanks a lot... :)

Hi bhanuprakash,

Your code works, but if lets say that I go from
One ----- > two ---- > threee... the text all remain there..
and when i go back to page one <---- and edit the name, that i change the name...then i go to page 2(two) and come back<--- to page one, the new text name that i have entered is not shown,
It takes the old text thats been typed b4...
How can it be solved..?
:(

Got it working..
I changed it to..

if (!isset($_SESSION['phone']) || isset($_POST['txt_phone'])) {
	$_SESSION['phone'] = $_POST['txt_phone'];
}

Got it working..
I changed it to..

if (!isset($_SESSION['phone']) || isset($_POST['txt_phone'])) {
	$_SESSION['phone'] = $_POST['txt_phone'];
}

With that if condition you minus well remove the if condition as it will allways pass.

Be a part of the DaniWeb community

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