Reputation: 103
I have 3 page, and pass value using $_POST
method. The reason not using $_GET
method is I don't want user go to the url and change the value and able to pass value. From the beginning for t2 and t3 page:
if(isset($_POST))
then save to the session.$_POST
data and read sessionIt is able to work when passing data from t1 to t2 and t2 to t3, but when click on back button from t3 to t2, the browser show "Confirm Form Resubmission"
. How can I make the page read the session data instead of the $_POST
value when user hit the back button?
t1.php
<html>
<body>
<form id="b">
value1: <input type="text" name="value1" id="value1">
value2: <input type="text" name="value2" id="value2">
<input type="button" onclick ="test()" value="test">
</form>
</body>
</html>
<script type="text/javascript">
function test(){
document.getElementById("b").method = "post";
document.getElementById("b").action = "t2.php";
document.getElementById("value1").value = "asd";
document.getElementById("value2").value = "zxc";
document.getElementById('b').submit();
}
</script>
t2.php
<?php
session_start();
if(isset($_POST)){
unset($_SESSION['t2']);
$_SESSION['t2']=$_POST;
}else if(isset($_SESSION['t2'])){
}else{
header("Location: http://localhost/my_project/t1.php");
}
?>
<html>
<body>
value1<?php echo $_SESSION['t2']["value1"]; ?>!<br>
value2 <?php echo $_SESSION['t2']["value2"]; ?>.
<form id="b">
value1: <input type="text" name="value1" id="value1">
value2: <input type="text" name="value2" id="value12">
<input type="button" onclick ="test()" value="test">
</form>
</body>
</html>
<script type="text/javascript">
function test(){
document.getElementById("b").method = "post";
document.getElementById("b").action = "t3.php";
document.getElementById("value1").value = "qwe";
document.getElementById("value2").value = "rty";
document.getElementById('b').submit();
}
</script>
t3.php
<?php
session_start();
if(isset($_POST)){
unset($_SESSION['t3']);
$_SESSION['t3']=$_POST;
}else if(isset($_SESSION['t3'])){
}else{
header("Location: http://localhost/my_project/t1.php");
}
?>
<html>
<body>
value1<?php echo $_SESSION['t3']["value1"]; ?>!<br>
value2 <?php echo $_SESSION['t3']["value2"]; ?>.
</body>
</html>
Upvotes: 3
Views: 2780
Reputation: 170
if youre losing data when you press navigater back button and as result the page is not loaded you can simply add this session_cache_limiter('private_no_expire');
before the session_start();
this will keep the page loaded as it is even if you press back button and as another way you can use get method and not post for passing parameters because the first method session_cache_limiter('private_no_expire');
will create some problems
Have a nice day
Upvotes: 1
Reputation: 1773
If you hit the "back" button in your browser, your browser tries to recreate the situation/status that was present when first calling that page.
That means, you do the following
1) t1 --> t2
2) t2 --> t3
3) Hit the "back" button in your browser
Now your browser tries to recreate situation 1) to display page t2 and by this wants to re-post the data you sent when you first called t2.
One way around this is to always load existing data from your session and to post your form via ajax to decouple the request of the next page and sending of data. If sending of data only happens when you execute that function (e.g. clicking the submit button which is bound to that function) from the browsers perspective there's no data needed to call the page.
If you follow this way, your page is alsways loaded with data that was stored in your session (and by this is the latest data). Only if you submit data via ajax your session data is updated.
Some options are explained in multiple answers to this question: jQuery AJAX submit form
I want to add some code snippet (jQuery/AJAX - not tested). It works this way:
Code Snippet:
// You can also reference the jQuery library differently than this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
// Will bind the function, once the page is loaded
$(document).ready(funtion(){
// Gets a reference to your form
var $form = $('#yourFormId');
// Gets the target URL of your form
var formURL = form.attr("action");
// Will be execured when you click the submit button
$form.submit(funtion(e){
// Does stop the form from submitting data itself
e.preventDefault();
// Call to send data
$.ajax({
type: "POST",
url: formURL,
data: $(this).serialize(),
cache: false,
success: function() { window.location.href = formURL; }
});
});
});
</script>
Upvotes: 1