Ok im a little new to php so please bare with me.
this is the code i have been using to Transfer Variables from and to. What i want to do is to keep the variables in page2 untill further notice even after submited. Concept of this for customer to fill in the blanks of the option he/she wants that way there is no coding for them.

page1.php

<form action="page2.php" method="get">

<FIELDSET>
<legend><P>Question 1</P></legend>

<FIELDSET>
<legend><P>Question# 1</P></legend>
<P>option 1: <input type="text" name="option1"/>
option 2: <input type="text" name="option2" /></P>
</FIELDSET>

<form action="" method="post" onsubmit="page2.php();return false;">

<input type="submit"value="question"/>

</form> 

Page2.php

<form method="get" action="page1.php">

<tr><td>Question 1:</td>
<td><fieldset>
<i><select name="FIGHT # 1">
<option value="Draw"selected>start
<option value="<?php echo $_GET["option1"]; ?>"><?php echo $_GET["option1"]; ?>
<option value="<?php echo $_GET["option2"]; ?>"><?php echo $_GET["option2"]; ?>
</select></i></td>
</tr>

<tr><td>&nbsp;</td><td><input type="submit" value="Submit"></td>
</tr></form>
</table>

Any help would be nice. Thank you

Recommended Answers

All 5 Replies

Can you be little bit clear in what you are asking for?

Absolutely, i would like the variable to stay in page2. Even after submit or refresh.
Not sure if sessions will work to do this or what..

Hi,

Session will work on this type, I mean even in a much more complicated applications. Session can even handle an array, and it will save the data in it , until it is unset -> removes the item in the session but not the session itself, OR session destroy-> which eventually destroy everything including the session.

Script below is something I wrote for testing. I used this for capturing a dynamic playlist and viewed videos history.. pretty much similar to what you see on youtube. I wrote this script to allow users to go back to items they already watched.

I tweaked it a little bit. I removed some of the items that will not make sense if included in this discussion. I also rewrite it for your needs. The only thing I did not do is to create two pages, and I used POST instead of GET.

This script is easy to modify. Again, data submitted through this form are not sanitized. It will be your responsibility to test it and mocked this script to your application.

The script is pretty much commented already, up to the point that I felt myself as being extremely becoming redundant.

Here is the code.. this will be the final codes I'll be writing for today. My wrists are feeling pretty bad now.

Save this as test.php, load the file on your server, and direct your favorite browser to it.

<?php
## first we create a session
session_start();

## second we create a function for storing our session as an array.
function createFormSession() {

    foreach($_POST as $input => $submitted) {
        $_SESSION['myform'][$input] = $submitted;
        }   
    }

## this is responsible for clearing the session if needed be
## Once again we check if the clear button has been clicked.
## if yes, we unset the session myform
if((isset($_POST["clear"])) && ($_POST['clear'] == "Clear History")) unset($_SESSION['myform']);    

## Third, we check if the form is submitted.
## Warning! this is a pretty basic validation..There are many more things to be added here. Before this can be suitable for production server.

if(isset($_POST['submit'])){
 createFormSession();

 $some_name = $_POST['somename'];
 $some_nickName = $_POST['somenickname'];



## I am checking here if the session myform already exist if does show the posted form data, or whatever is suitable for your application.

if(isset($_SESSION['myform'])){

## here I disassembled the myform session into its idividual parts  
    $fname = $_SESSION['myform']['somename'];
echo " Your form was Submitted with a name called: ".$fname;
echo "<br/>";

$fnickname = $_SESSION['myform']['somenickname'];
echo "Your form was Submitted with a nickname of: ".$fnickname;

echo "<br/>";

## we give our user the option to cancel or break the session

echo '<form action="" method="post"> <input type="submit" name="clear" value="Clear History"></form><br/>'; 
}
} 
else
{
## fourth, if the form is not submitted, 
## we show our form
?>
<form method = "post" action = "">
<label>Type your name:      </label><input name="somename" />
<br/>
<br/>
<label>Type your nick name: </label><input name="somenickname" /><br/>
<input type="submit" name="submit" value="Submit"/>
<br/>
</form>
<?php
}

?>

Brief explanation of the codes above..
Notice on top of the page, there is a function there? This function is responsible in putting the inputted data on our form. So, whatever the client typed and submitted will be placed in a session called myform.

Below it the session unset function.. This is responsible for unsetting the session in case the client changed his/her mind to proceed or just want a reset.

Next is the form validation, I used isset here to see if the form has been submitted. If this returned true, we triggered the createdFormSession function to set the data in session array.

Below it is another set of condition. The condition is to check if the session myform exist, and if it does exist, we show array from the session itself. Here you will also see the clear history form. Once this button is click, the items in session are completely removed, but NOT the session myform. This still exist, without data in it.

Try the script by running it on your server.. type in a name and a nickname, and then hit submit. The script should demonstrate what you are trying to achieved.

To use it in your application, just replace the POST to GET, and then move the form validation to your page two.

thank you veedeoo,
hmm not sure if im doing this right cause when i reshresh the page the variables disapear.

try making a second page called sessiondump.php

<?php
session_start();
var_dump($_SESSION);
?>

and navigate to that on the same server after submitting the form veedeoo posted.

you need to make sure session_start() is called at the start on any page you want session vars, then they are available in the $_SESSION array for the length of your visit on the website(based on your servers session settings)

The other option is cookies which are set by setcookie("option1",$option1,time()+3600);

then you access them with $_COOKIE['option1']

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.