Filling in form fields when going back and forth webpages
I've looked around (here at stackoverflow and with Google) but I can't seem to figure out why my form values disappear when I go backwards and then forwards on a web survey. I have three pages I'm experimenting with. If I go through the survey answering each question, and then go backwards the values are preserved. But, my values for the second and third page are no longer filled in if I re-answer the question on the first page and press submit. However, the array listed out at the top still shows it. Shouldn't the PHP code in the form itself repopulate the values from the session array until a user changes them?
I'm also aware that the method I'm using for storing all POST values into a session is dangerous but I plan to only write this to a plain .txt file. Thank you for any help!
Page 1
<?php
// begin the session
ini_set('session.cache_limiter', 'private');
session_start();
// Take each input name and create a variable for it
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
?>
<html>
<head>
<title>This is a test</title>
</head>
<body>
<form method="POST" name="Gender" action="page2.php">
<p>What is your gender?</p>
<input type="radio" name="gender" value="male" id="male" <?php if (isset($_SESSION['male'])) { echo 'checked="checked"'; } else { echo $gender = ''; } ?>> <label for="male">Male</label> <br />
<input type="radio" name="gender" value="female" id="female" <?php if (isset($_SESSION['female'])) { echo 'checked="checked"'; } else { echo $gender = ''; }?>> <label for="female">Female</label><br />
<input type="submit">
</form>
</body>
</html>
Page 2
<?php
// begin the session
ini_set('session.cache_limiter', 'private');
session_start();
// Take each input name and create a variable for it
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
// Prints out the session array
print('<input type="hidden" name="' . $k . '" value="' . $v . '" />');
Print_r ($_SESSION);
?>
<html>
<head>
<title>This is a test</title>
</head>
<body>
<form method="POST" name="lolz" action="page3.php">
<p>What is your lolz?</p>
<input type="radio" name="lolz" value="YesLOLZ" id="YesLOLZ" <?php if (iss开发者_如何学Pythonet($_SESSION['YesLOLZ'])) { echo 'checked="checked"'; } else { echo $lolz = ''; } ?>> <label for="YesLOLZ">YesLOLZ</label> <br />
<input type="radio" name="lolz" value="NoLOLZ" id="NoLOLZ" <?php if (isset($_SESSION['NoLOLZ'])) { echo 'checked="checked"'; } else { echo $lolz = ''; } ?>> <label for="NoLOLZ">NoLOLZ</label><br />
<input type="submit">
</form>
</body>
</html>
Page 3
<?php
// begin the session
ini_set('session.cache_limiter', 'private');
session_start();
// Take each input name and create a variable for it
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
// Prints out the session array
print('<input type="hidden" name="' . $k . '" value="' . $v . '" />');
Print_r ($_SESSION);
?>
<html>
<head>
<title>This is a test</title>
</head>
<body>
<form method="POST" name="lolz" action="page3.php">
<p>What is your roflz?</p>
<input type="checkbox" name="roflz[]" value="Yesroflz" id="Yesroflz" <?php if (isset($_SESSION['Yesroflz'])) { echo 'checked="checked"'; } else { echo ''; } ?>> <label for="Yesroflz">Yesroflz</label> <br />
<input type="checkbox" name="roflz[]" value="Noroflz" id="Noroflz" <?php if (isset($_SESSION['Noroflz'])) { echo 'checked="checked"'; } else { echo ''; } ?>> <label for="Noroflz">Noroflz</label><br />
<input type="submit">
</form>
</body>
</html>
Your sessions are not working because the headers already have been sent. You are outputting html before the session_start
statement and you can´t do that if you want to use sessions.
Just move your html
tag down to below the first php block and make sure there are no spaces or new-lines before the opening php tag.
Edit: Based on your comments, I think you need to do the following at the start of all php files of the form:
- start and read the session
- read the
$_POST
values and add / overwrite them in your$_SESSION
variables
(the session now contains the newly posted and previously posted information) - use
$_SESSION
instead of$_POST
variables in your form where you set the values, so:<?php if (isset($_SESSION['YesLOLZ'])) { echo 'checked="checked"'; }
instead of:<?php if (isset($_POST['YesLOLZ'])) { echo 'checked="checked"'; }
Also note that the variables $k
and $v
do not really exist outside your while
loop; they do exist, but contain information of the last $_POST
variable, probably the send button so you can't really use them.
If this doesn't work, please screenshot/copy+paste what appears in response to print_r($_SESSION);
& report any PHP errors you get, even if they're just notice-level.
So far you've done the following:
- Starting a session has to be the very first thing on the page to be successful. You should have been getting the error Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent in ... By moving the
session_start()
up this was fixed. - You are copying the $_POST array into $_SESSION on every page.
- You ensured that you were consistently using the $_SESSION superglobal rather than the $_POST superglobal when checking for values.
Now you need to:
- Ensure you are using a
foreach
loop and the appropriate variable name to output hidden inputs. You should be getting an error that reads Notice: Undefined variable: k in line 9 ... etc. - Ensure you are using the correct array key when checking for the value of a field. The key in the $_POST (and therefore $_SESSION) array will match the
name
attribute of theinput
while the value will match thevalue
attribute of theinput
.
If you haven't gotten any of these errors try adding error_reporting(E_ALL);
to line 5 (right after session_start();
). This line should be used only during development and taken out when you release the code.
Page 1
<?php
// begin the session
ini_set('session.cache_limiter', 'private');
session_start();
// Take each input name and create a variable for it
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
// Prints out the session array
print_r($_SESSION);
?>
<html>
<head>
<title>This is a test</title>
</head>
<body>
<form method="POST" name="Gender" action="page2.php">
<p>
What is your gender?
</p>
<input type="radio" name="gender" value="male" id="male" <?php if (isset($_SESSION['gender']) && $_SESSION['gender'] == "male") { echo 'checked="checked"'; } ?>>
<label for="male">
Male
</label>
<br />
<input type="radio" name="gender" value="female" id="female" <?php if (isset($_SESSION['gender']) && $_SESSION['gender'] == "female") { echo 'checked="checked"'; } ?>>
<label for="female">
Female
</label>
<br />
<input type="submit">
</form>
</body>
</html>
Page 2
<?php
// begin the session
ini_set('session.cache_limiter', 'private');
session_start();
// Take each input name and create a variable for it
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
// Prints out the session array
print_r($_SESSION);
?>
<html>
<head>
<title>This is a test</title>
</head>
<body>
<form method="POST" name="lolz" action="page3.php">
<?php
foreach($_SESSION as $k=>$v) {
echo '<input type="hidden" name="' . $k . '" value="' . $v . '" />';
}
?>
<p>
What is your lolz?
</p>
<input type="radio" name="lolz" value="YesLOLZ" id="YesLOLZ" <?php if (isset($_SESSION['lolz']) && $_SESSION['lolz'] == "YesLOLZ") { echo 'checked="checked"'; } ?>>
<label for="YesLOLZ">
YesLOLZ
</label>
<br />
<input type="radio" name="lolz" value="NoLOLZ" id="NoLOLZ" <?php if (isset($_SESSION['lolz']) && $_SESSION['lolz'] == "NoLOLZ") { echo 'checked="checked"'; } ?>>
<label for="NoLOLZ">
NoLOLZ
</label>
<br />
<input type="submit">
</form>
</body>
</html>
Page 3
<?php
// begin the session
ini_set('session.cache_limiter', 'private');
session_start();
// Take each input name and create a variable for it
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
// Prints out the session array
print_r($_SESSION);
?>
<html>
<head>
<title>This is a test</title>
</head>
<body>
<form method="POST" name="lolz" action="page3.php">
<?php
foreach($_SESSION as $k=>$v) {
echo '<input type="hidden" name="' . $k . '" value="' . $v . '" />';
}
?>
<p>
What is your roflz?
</p>
<input type="checkbox" name="roflz" value="Yesroflz" id="Yesroflz" <?php if (isset($_SESSION['roflz']) && $_SESSION['lolz'] == "Yesroflz") { echo 'checked="checked"'; } ?>>
<label for="Yesroflz">
Yesroflz
</label>
<br />
<input type="checkbox" name="roflz" value="Noroflz" id="Noroflz" <?php if (isset($_SESSION['roflz']) && $_SESSION['lolz'] == "Noroflz") { echo 'checked="checked"'; } ?>>
<label for="Noroflz">
Noroflz
</label>
<br />
<input type="submit">
</form>
</body>
</html>
The problem is that you are storing the answers in the session but not checking it when you want to see if you should automatically fill in a certain input. The post values are not persistent so if you go back and restart the quiz from page one then only the values from page one will be in the post variable. When you submit page two, you actually lose everything from the POST in page one but since you are storing it in the session you still have access to it.
精彩评论