Reputation: 117
A user submit a form and goes from page_0.php to page_1.php. Then goes through a link from page_1.php to page_2.php. If the user want to press the "back" button from the browser, How is it possible to prevent a warning message from the browser like "Error code: ERR_CACHE_MISS" (confirm form submit?) and also load the same data that was displayed before pressing the link to page_2?
I tried using session, but not always work:
page_0.php: the user selects an option:
<!-- this is page_0.php -->
<form action="page_1.php" method="post">
<select name="id">
<option value="1">Car</option>
<option value="2">Bike</option>
<option value="3">Skate</option>
<option value="4">Plane</option>
</select>
<button type="submit">Submit</button>
</form>
page_1.php: (after submit the form located on page_0.php, the user arrives to page_1.php)
<?php
session_start();
//this is page_1.php
if(isset($_POST['id']))
{
$product_id = $_POST['id'];
$_SESSION['data'] = $_POST['id'];
}
else
{
$product_id = $_SESSION['data'];
}
$result = mysqli_query($con, "SELECT id_product_name, product_name FROM products WHERE id='$product_id'");
$row = mysqli_fetch_assoc($result);
while ($row=mysqli_fetch_array($result))
{?>
<a href="page_2.php?id=<?php echo $row['id_product_name']?>">Go to product detail</a>
<?php
}
?>
Now the user clicks on one of the links to see the further details of a product. Let say that clicks on the link with id=3:
<a href="page_2.php?id=3">Go to product detail</a>
The user arrives to page_2.php.
<?php
//this is page_2.php
$product_id = $_GET['id'];
echo 'Hello here you can find details of product'.$product_id;
?>
Now the user press the "back" button on the browser.
My question is: How can the user go back to page_1.php and still view the same information that he was seeing before clicking on the link and also without getting any message from the browser?
Upvotes: 1
Views: 2644
Reputation: 74216
Echo the session variable in the form's input value.
Sidenotes:
Part 1
<?php
session_start();
?>
<form action="page_1.php" method="post">
<input value="<?php echo $_SESSION['data']; ?>" name="id" />
<button type="submit">Submit</button>
</form>
If that isn't the desired result, then just use the session variable on the same page.
I.e.:
$data = $_SESSION['data'];
echo $data;
and keeping your present input as <input value="854" name="id" />
Part 2
<?php
session_start();
if(isset($_POST['id']))
{
$product_id = $_POST['id'];
$_SESSION['data'] = $_POST['id'];
}
else
{
$product_id = $_SESSION['data'];
$result = mysqli_query($con, "SELECT product_name FROM products WHERE id='$product_id'");
$row = mysqli_fetch_assoc($result);
echo $row['product_name'];
}
?>
<a href="page_2.php">Go to page 2</a>
Edit:
Page 2 would look something like this, assigning $_SESSION['data']
to $_GET['id']
:
<?php
//this is page_2.php
session_start();
$_GET['id'] = $_SESSION['data'];
$product_id = $_GET['id'];
echo 'Hello here you can find details of product: '.$product_id;
?>
Upvotes: 0
Reputation: 8659
You should always redirect serverside from a page that handles a form submission, so it should not possible to get back to it from the back button. This is to prevent the possibility of accidental double submission on back button, as well as ugly errors like the one you noticed.
(Just to clarify. What this does is make the history skip the page that handles the submission, so that hitting back from page_2 will take you directly back to the form, not the handling page.)
So, change the end of your page_1.php from
?>
<a href="page_2.php">Go to page 2</a>
To:
header('Location: page_2.php');
exit;
Also, for this to work, you may have to make sure not to print anything out before the call to header()
. I don't remember if that's necessary in PHP or not, but it is in some languages (e.g. Java/JSP).
Upvotes: 0