[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1748
  • Last Modified:

remember form values using PHP session

I have a PHP webpage with a form with 6 values.  A submit button passes these 6 values as parameters via GET method to another PHP file.
After I press the browser "back" button, these 6 values appear empty.
How can I make them appear like they was the last time instead than empty?

Someone told me that there is a solution with cookie and a solution via PHP session. I'm interested in the solution via PHP session.
0
lucavilla
Asked:
lucavilla
  • 5
  • 5
1 Solution
 
lucavillaAuthor Commented:
I looked at those pages but they seems complete guides to PHP session while I only need a once-in-a-life solution for a single webpage.
Do you know a solution described in less than 1 page?
0
 
shobinsunCommented:
Hi,

This simple example will help you:


<?php
session_start();
?>
<html>
<head>
<title>Simple HTML Form</title>
</head>
<body>
<form action='session1.php' method='post'>
<input type='text' name='name' value="<?php echo $_SESSION['userName'];?>">
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>
 
session1.php:
 
<?php
session_start();
$_SESSION['userName']='shobin';
?>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
agamalCommented:
what happen tested or what >?
0
 
lucavillaAuthor Commented:
I tested the shobinsun's code but it doesn't show the text that the user entered last time.  It always show the text "shobin". How can I make it show the text that the user entered last time?
0
 
shobinsunCommented:
Hi,

Use this in session1.php:

<?php
session_start();
$_SESSION['userName']=$_POST['name'];
?>
0
 
lucavillaAuthor Commented:
beautiful, it works!
but it doesn't work if I choose the GET method.
I need necessarily to use the GET method because I want to show the 6 parameters in the URL.
How can I make it work with the GET method?
0
 
shobinsunCommented:
Hi,

I am using this:

<?php
session_start();
?>
<html>
<head>
<title>Simple HTML Form</title>
</head>
<body>
<form action='session1.php' method='get'>
<input type='text' name='name' value="<?php echo $_SESSION['userName'];?>">
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>

<?php
session_start();
$_SESSION['userName']=$_GET['name'];
?>


What you did try?

Please send me the code.
0
 
lucavillaAuthor Commented:
I should apply it here: http://alturl.com/74oj

It should remember the six entered values after the user submit them to get the results page and come back to the homepage.
 
0
 
shobinsunCommented:
Hi

you want this:


<?php
session_start();
?>
<html>
<head>
<title>Simple HTML Form</title>
</head>
<body>
<form action='session.php' method='get'>
<input type='text' name='name' value="<?php echo $_SESSION['userName'];?>">
<input type='text' name='name1' value="<?php echo $_SESSION['userName1'];?>">
<input type='text' name='name2' value="<?php echo $_SESSION['userName2'];?>">
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>
<?php
$_SESSION['userName']=$_GET['name'];
$_SESSION['userName1']=$_GET['name1'];
$_SESSION['userName2']=$_GET['name2'];
 
?>

Open in new window

0
 
lucavillaAuthor Commented:
I didn't have time to test it but I think that it is what I will need. Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now