Jump to content

need help passing variable from 2nd to 3rd page


kannu

Recommended Posts

Hello,

I passed the variable from 1st to 2nd page successfully with the following code:

1st page code:-

<form action="a2008.php" method="POST">

<input type="hidden" id="answer" name="answer" value="<?php echo $_SESSION["answer"] ?>"/>

<input type="image" name="submit" src=button.jpg" />

</form>

 

2nd page code to retrieve the variable:-

<?

$answer=$_POST['answer'];

 

if ($answer !="layaway") {

  $answer = "complete";

}

echo $answer;

?>

 

This retrieves the value successfully. Now, I need to pass it to the third page and I use the following code as used before, but it does not work:-

<form action="conf.php" method="POST">

<input type="hidden" name="answer" value="<?php echo $_SESSION["answer"] ?>"/>

<input type="image" name="submit" src="button.jpg" >

</form> 

 

When I try to retrieve it on the 3rd page, I do not get any value. Can you please help me out at this. I have spent over 5 hours to work this out, and I dont know how, I am new to the php coding.

 

thanks

I am posting the same question again, using the code tags, so that it is easier to understand. I have the code session_start() at the beginning of my script already. My question is, if someone can help me:-

 

Hello,

I passed the variable from 1st to 2nd page successfully with the following code:

1st Page code

<form action="a2008.php" method="POST">
<input type="hidden" id="answer" name="answer" value="<?php echo $_SESSION["answer"] ?>"/>
<input type="image" name="submit" src=button.jpg" />
</form>

 

 

2nd page code to retrieve the variable:-

<?
$answer=$_POST['answer'];
echo $answer;
?>

 

This retrieves the value successfully. Now, I need to pass it to the third page and I use the following code as used before, but it does not work:-

 

<form action="conf.php" method="POST">
<input type="hidden" name="answer" value="<?php echo $_SESSION["answer"] ?>"/>
<input type="image" name="submit" src="button.jpg" >
</form>  

 

When I try to retrieve it on the 3rd page, I do not get any value. Can you please help me out at this. I have spent over 5 hours to work this out, and I dont know how, I am new to the php coding.

 

thanks

on second page that links to page 3 add this

 

 YOURLINKYOUR.php?answer=$answer 

 

and the on page 3

 

<form action="conf.php" method="POST">
<input type="hidden" name="answer" value="<?php echo $_GET["answer"] ?>"/>
<input type="image" name="submit" src="button.jpg" >
</form>  

$_SESSION['answer'] should still contain the value from the beginning, in your snippets I don't see it being updated/deleted anywhere.

 

As flyhoney suggested, are you sure session_start() is at the top of all 3 forms?  Sessions and form POST data have nothing to do with each other.

 

You can retrieve a session variable regardless of a form as long as the current page is part of the session (session_start()).

 

Passing post variables in a form chain would be like:

form1

<form action="form2.php" method="post">
<input type="text" name="passMe1" /> //<- user inputs "5"

form2

<form action="form3.php" method="post">
<input type="text" name="passMe2" value="<?php echo $_POST['passMe1']; ?>" />//<-- shows 5

form3

<form action=""  method="post">
<input type="type" name="final" value="<?php echo $_POST['passMe2']; ?>" /> //<-- shows 5

 

----------------------------

sessions

 

page1.php

<?php
session_start();
$_SESSION['passMe'] = 'Hello World';
?>

anyotherpage.php

<?php
session_start();
echo $_SESSION['passMe']; //outputs Hello World
?>

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.