Hey guys:
I am trying to make this form to clear after being submited. As it is now the form submits, however, the input fields do not clear and when you refresh the page it ask you if you want to resubmit. Can anyone help?
<?php
// Message Vars
$msg = '';
$msgClass = '';
// Check For Submit
if(filter_has_var(INPUT_POST, 'submit')){
// Get Form Data
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
// Check Required Fields
if(!empty($email) && !empty($name) ){
// Passed
// Check Email
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
// Failed
$msg = 'Please use a valid email';
$msgClass = 'alert-danger';
} else {
// Passed
$toEmail = 'email@email.com';
$subject = 'From '.$name;
$body = '<h2>New Subscribe</h2> <h4>Name</h4><p>'.$name.'</p> <h4>Email</h4><p>'.$email.'</p>
';
// Email Headers
$headers = "MIME-Version: 1.0" ."\r\n";
$headers .="Content-Type:text/html;charset=UTF-8" . "\r\n";
// Additional Headers
$headers .= "From: " .$name. "<".$email.">". "\r\n";
if(mail($toEmail, $subject, $body, $headers)){
// Email Sent
$msg = 'Success';
$msgClass = 'alert-success';
} else {
// Failed
$msg = 'Try Again';
$msgClass = 'alert-danger';
}
}
} else {
// Failed
$msg = 'Please fill in all fields';
$msgClass = 'alert-danger';
}
}
?> <!-- NEWSLETTER SECTION --> <section id="newsletter" class="text-center p-5 bg-dark text-white"> <div class="container"> <?php if($msg != ''): ?> <div class="alert <?php echo $msgClass; ?>"><?php echo $msg; ?></div> <?php endif; ?> <div class="row"> <div class="col"> <h2>Subscribe</h2> <p class="text-center">Your email will never be shared.</p> <form class="form-inline justify-content-center" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label class="sr-only" for="name">Name</label> <input type="text" name="name" class="form-control mb-2 mr-sm-2 mb-sm-0" placeholder="Name" value="<?php echo isset($_POST['name']) ? $name : ''; ?>"> <label class="sr-only" for="email">Email</label> <input type="text" name="email" class="form-control mb-2 mr-sm-2 mb-sm-0" placeholder=" Email" value="<?php echo isset($_POST['email']) ? $email : ''; ?>"> <button class="btn btn-primary" type="submit" name="submit">Submit</button> </form> </div> </div> </div> </section>