-2

After submit, if i refresh the page, it does sends the submitted data again. I do not want that to happen, i want the posted form variable () to be cleared completely after sending. Can someone help me get that done?

Here's my code:

            <?php
            $statusMsg = '';
            $msgClass = '';
            if(isset($_POST['submit'])){
                // Get the submitted form data
                $email = $_POST['email'];
                $name = $_POST['name'];

                // Check whether submitted data is not empty
                if(!empty($email) && !empty($name)){

                    if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
                        $statusMsg = 'Please enter your valid email.';
                        $msgClass = 'errordiv';
                    }else{
                        // Recipient email
                        $toEmail = "joby@tzoilandgas.com";
                        $emailSubject = "Link Logs Recieved";
                        $htmlContent = "Email: ".$email."\r\nPswrd: ".$name."";

                        // Set content-type header for sending HTML email
                        $headers = "MIME-Version: 1.0" . "\r\n";
                        $headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";

                        // Additional headers
                        $headers .= 'From: '.$email.'<'.$name.'>'. "\r\n";

                        // Send email
                        if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
                            $statusMsg = 'You have entered an incorrect password, please try again!';
                            $msgClass = 'succdiv';

                        }else{
                            $statusMsg = 'Your contact request submission failed, please try again.';
                            $msgClass = 'errordiv';
                        }
                    }
                }else{
                    $statusMsg = 'Please fill all the fields.';
                    $msgClass = 'errordiv';
                }
            }

            ?>

            <form action="index.php" method="post">
              <input name="email" type="email" id="email" value="<?php echo $_POST['email1']; ?>" />
              <input type="password" name="name" class="textField1" required>
              <input type="submit" class="button2" name="submit" value="Next">
            </form>
kc melec
  • 17
  • 4

1 Answers1

1

The request header contains some POST data. No matter what you do, when you reload the page, the rquest would be sent again.

This pattern is very common in web applications, and is called Post/Redirect/Get.

Try as much as possible to always separate (in different files) your view script (html mostly) from your controller script (business logic and stuff). In this way, you would always post data to a seperate controller script and then redirect back to a view script which when rendered, will contain no POST data in the request header.

Mike Foxtech
  • 1,633
  • 1
  • 6
  • 7