0

I have a webpage with a form on it.

I use this php code to put the value of the form in sessions when de _POST method is activated:

<?php session_start(); ?>
<?php
$dump ='';
$dump .='<!DOCTYPE html>
<html>
<head></head>
    <body>';


$dump .='   
    <h1>Deel 1: registratiegegevens</h1>
<form action="oplossing21-deel2.php" method="post">

<ul>
        <li>            <label for="email">email: </label><input type="text" name="email" id="email" value="'.$email.'" />
        </li>
                    <br />
        <li>            <label for="nickname">nickname: </label><input type="text" name="nickname" id="nickname" />
        </li>
                    <br />
        <li>            <input type="submit" value ="volgende" />
        </li>


</ul>
                </form>

';  




if ($_POST) {
  // Store our name in the session array
  $_SESSION["email"] = $_POST["email"];
  $_SESSION["nickname"] = $_POST["nickname"];
}

$email = isset($_SESSION["email"]) ? $_SESSION["email"] : "leeg";




    $dump .='   

    </body>
</html>';
echo $dump;

?>    

Now i want to know how i can make sure that if i reload the page (F5 or cmd+r), that the fields are filled in with the last typed values. So i submit the form, and i go to the next page, but when i come back, i want the input fields to be filled in with the last known value. Can someone help me with this? I already tried to do something with the isset() - function but i don't know where to go from here.

on the second page i just put the sessions on the html file, and that works..

<li>'.$_POST["email"].'</li> 
1
  • Have u figured it out? Commented Apr 9, 2013 at 17:52

5 Answers 5

1

the better approach will use html5 storage on onkeyup event so even if reload it will still in local storage but if you can not do this in only php that time you need to use ajax and make ajax call on after some interval or onkeyup..etc event

Sign up to request clarification or add additional context in comments.

3 Comments

I have to use php in this case
what the problem with only html is that if you accidentally refresh form without submit it data will loss
It's because it's a school assignment, so we need to learn various things about php. I prefer HTML to.
0

You can do this by checking if the variables are set during pageload. For example:

PHP
$email = isset($_SESSION["email"]) ? $_SESSION["email"] : "";

and in

HTML
<input type="text" value="<?php echo $email; ?>" />

So if the session-variable is set, the contents of it are being inserted into the field - if not, then an empty string is inserted.

6 Comments

This should work. Are you sure the information is stored in the variable and that the information is there in the first place? Make a dump of the post var echo '<pre>'; print_r($_POST); and of the variable you are using to display the value.
I've got a second php page, on this page i print the email and nickname on the html, so there they are shown. But when i go back (previous) and then reload the page ( so i'm on first page) then the inputfields are back empty
I don't know if I understand you correctly, but if I do: The variable $email has to be set on every page you want to display the contents. Only the $_SESSION Variables are carried over, the rest ist reset. So if you want to display the email on the first page, you need to set the variable there as well like this: $email = isset($_SESSION["email"]) ? $_SESSION["email"] : "";
I'm going to edit the code to my full code, it's not that much
try $_SESSION["email"] = isset($_POST["email"])?$_POST["email"]:''; instead of only if ($_POST)
|
0

Anytime you want to use $_POST, you need to put it in the "action" document the "form" tag calls. The document cannot store a value before the form data is sent to the server. In the action document "oplossing21-deel2.php" is where you put $_SESSION["email"] = $_POST["email"]; and $_SESSION["nickname"] = $_POST["nickname"];

Comments

0

in your form method file oplossing21-deel2.php use <?php session_start(); ?> then, do

$_SESSION["email"] = isset($_POST["email"]) ? $_POST["email"] : "";

it will save the email value into the session.

When you return back to your form file

$email = isset($_SESSION["email"]) ? $_SESSION["email"] : "";

<input type="text" value="<?php echo $email; ?>" />

Also, when you successfully used the values in your form-method file, clear the session $_SESSION["email"]=""; so the old value won't be there when you reload the form again.

Comments

0

on the page where your form is submitting do something like this

session_start();
$_SESSION['data'] = $_POST['data'];
$_SESSION['data_another'] = $_POST['data_another'];

and than you can access those session variables any where like this

session_start(); // this should be at the top of the page before any html load
<input type="text" name="name" value="<?php echo $_SESSION['data'];?>"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.