Reputation: 101
I have read a few other posts and try to put them together but still could not solve my problem: Navigating back with PHP form submission, php back button
I'm writing template for a long online form, and I'm trying to break the form into several pages. And no matter if users click "back" or "next", the previously entered fields will still remain there.
However, whenever I click on the back button on the second page, all the info on the first page goes away.
This is the php block on top of the 1st page:
session_start();
$_SESSION['initialized'] = 'Aquafina';
if (!isset($_SESSION['email'])) {
$_SESSION['email'] = '';
}
if (!isset($_SESSION['fname'])) {
$_SESSION['fname'] = '';
}
foreach ($_SESSION as $key => $value) {
$temp = trim($value);
${$key} = $temp;
}
HTML of the 1st page:
<form method="post" action="secure.php">
<p>
<label for="email">Email: </label>
<input type="text" name="email" value="<?php htmlentities($email); ?>">
</p>
<p>
<label for="fname">First Name: </label>
<input type="text" name="fname" value="<?php htmlentities($fname); ?>">
</p>
<p>
<label for="hidden"> </label>
<input type="submit" name="next" value="NEXT">
</p>
</form>
I've tried many alternatives, and currently I'm stuck on the notion that I'll pass the form into a page call "secure.php" (I'm not sure why I did this in the first place), and has the code of the following:
session_start();
foreach ($_POST as $key => $value) {
$_SESSION[$key] = trim($_POST[$key]);
}
header('Location: form2.php' );
The second page looks similar, but I added a back button that caused all the problems. The php block on top of 2nd page:
session_start();
if (isset($_SESSION['initialized'])) {
if (!isset($_SESSION['lname'])) {
$_SESSION['lname'] = '';
}
if (!isset($_SESSION['university'])) {
$_SESSION['university'] = '';
}
foreach($_SESSION as $key => $value) {
$temp = trim($value);
${$key} = $temp;
}
} else {
header('Location: form.php');
exit;
}
The HTML on the 2nd page:
<form method="post" action="secure2.php">
<p>
<label for="lname">Last Name: </label>
<input type="text" name="lname" value="<?php htmlentities($lname); ?>">
</p>
<p>
<label for="university">University: </label>
<input type="text" name="university" value="<?php htmlentities($university); ?>">
</p>
<p>
<label for="hidden"> </label>
<button class="back"><a href="form.php">Back</a></button>
<input type="submit" name="review" value="REVIEW">
</p>
</form>
Thank you so much whoever can help!!!!!
Upvotes: 2
Views: 198
Reputation: 14921
I don't think the problem is your back button. The problem is probably because you're not echo-ing anything in your inputs' values.
To fix it, replace
<input type="text" name="email" value="<?php htmlentities($email); ?>">
With
<input type="text" name="email" value="<?php echo htmlentities($email); ?>">
OR
<input type="text" name="email" value="<?=htmlentities($email)?>">
And do the same thing for every of your inputs. If you didn't notice what I've added, I've added the echo
in the value attribute of your HTML.
Upvotes: 4