0

I have searched all over the internet but I am still confused. I have a form which I submit and redirects to its self. It then echoes the variables to the screen. But how can I prevent form resubmission after the user hits the refresh button?? Is this achieveable and how??? Here is the code

            <!DOCTYPE HTML> 
        <html>
        <head>
        </head>
        <body> 

        <?php
        // define variables and set to empty values
        $name = $email = $gender = $comment = $website = "";

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
           $name = test_input($_POST["name"]);
           $email = test_input($_POST["email"]);
           $website = test_input($_POST["website"]);
           $comment = test_input($_POST["comment"]);
           $gender = test_input($_POST["gender"]);
           echo "<h2>Your Input:</h2>";
        echo $name;
        echo "<br>";
        echo $email;
        echo "<br>";
        echo $website;
        echo "<br>";
        echo $comment;
        echo "<br>";
        echo $gender;
        }
        if($_SERVER["REQUEST_METHOD"] == "GET")
        {
            header('Location: '. $_SERVER['PHP_SELF'] , true, 303);
        }

        function test_input($data) {
           $data = trim($data);
           $data = stripslashes($data);
           $data = htmlspecialchars($data);
           return $data;
        }
        ?>

        <h2>PHP Form Validation Example</h2>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
           Name: <input type="text" name="name">
           <br><br>
           E-mail: <input type="text" name="email">
           <br><br>
           Website: <input type="text" name="website">
           <br><br>
           Comment: <textarea name="comment" rows="5" cols="40"></textarea>
           <br><br>
           Gender:
           <input type="radio" name="gender" value="female">Female
           <input type="radio" name="gender" value="male">Male
           <br><br>
           <input type="submit" name="submit" value="Submit"> 
        </form>

I am new to this.... Please help.

user3485417
  • 583
  • 2
  • 7
  • 21
  • You should simply stop that strange habit to have a form submit to the script that generated the form. I never understood why that is done. It causes tons of issues. You have one action that generates a view, the form, and another action that processes the form. So two separate scripts. Easy, efficient and transparent. – arkascha Jan 23 '16 at 14:52
  • Even if I stop submitting to the same script, the form keeps resubmitting on page refresh. Could you please advice how to stop that?? And how should I show error messages to the user when submitting wrong data??? – user3485417 Jan 23 '16 at 15:19

1 Answers1

0

Simply, if post values are exists, don't show submit button or remove behavior of submit button like this

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
       Name: <input type="text" name="name">
       <br><br>
       E-mail: <input type="text" name="email">
       <br><br>
       Website: <input type="text" name="website">
       <br><br>
       Comment: <textarea name="comment" rows="5" cols="40"></textarea>
       <br><br>
       Gender:
       <input type="radio" name="gender" value="female">Female
       <input type="radio" name="gender" value="male">Male
       <br><br>
       <?php if(!isset($_POST["name"])) { ?>
       <input type="submit" name="submit" value="Submit">
       <?php } ?>
    </form>
Alfred Woo
  • 648
  • 1
  • 6
  • 23