防止用户在提交后返回PHP表单[关闭]

I have 2 PHP forms. If a user submits the first form he should be redirected to the second form.

I would like to prevent users from going back to the first form after submitting it. As of now, after I submit the first form, it will redirect me to the second form but if I press the back button in the browser, I can see all the data I inserted in the first form.

(I used header ('Location: form2.php') at the end of my PHP file to redirect to second form and it works properly. I searched a lot about preventing from resubmitting the form but still I could not solve my problem.

I would personally have the form be on the same page (aka form.php), but control the output on the page based on variables that you pass through the form.

On the first form, you could put a hidden var that tells it which form has been completed.

ex: <input type="hidden" name="task" value="form1" />

this way, when you pass the variables to the second form, you can have control over the variables passed and provide the propper output... ex.

<?php
if ($_POST['task'] == 'form1') {
  // show form2
} else {
  // show form1
}
<?

Then again, this is just based on what you described. Depending on what the forms are for might change the solution. Also, if you want to have the users not be able to go back to form1 until they have finished the form, then you would want to save the submitted form variables.

<?php
session_start();
if ($_POST['task'] == 'form1') {
  $_SESSION['blah'] = $_POST['blah'];
  $_SESSION['form1'] = true;
}
<? 

At this point, the check to see what form they were on would be similar to:

<?php
if ($_SESSION['form1']) {
  // show form 2
}
<? 

Just a warning, I haven't debugged any of this code so it might not work as-is.

Maybe this is what you want, This is the main idea.

  1. Create 2 form on the same page, but hide the second form.

  2. When first form's submit button clicked, send the data with ajax, hide the first form and show the second form.

    <html>
    <head>
    </head>
    <body>
            <div class='form1'>
                Form1<br>
                <input id='name'>
                <button id='btnGo'>Send</button>
            </div><div class='form2' style='display:hidden;'>
                Form2<br>
                <input id='location'>
                <button id='btnGo2'>Send</button>
            </div>
    
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
    
        <script>
        $(document).ready(function() {
            $(".form2").hide();
        });
    
        $("#btnGo").click(function() {
            $(".form1").hide();
            $(".form2").show();
            var name = $("#name").val();
                $.ajax({
                    type: "POST",
                    url: "process.php",
                    data: { name: name},
                });
    
            event.preventDefault();
        });
    
        $("#btnGo2").click(function() {
                var loc = $("#location").val();
                $.ajax({
                    type: "POST",
                    url: "process.php",
                    data: { location: loc }
                });
    
        });
        </script>
    </body>