问题
I'm creating a website for a friend, my first website. Currently have it up and hosted too.
Created a basic "Contact Me" form, where a user can fill out information, then it will send an email.
The question is: upon clicking submit, how do I get the webpage to redirect back to the index.html after a "Your form has been submitted" response from php.
Here is html form section:
<div class="contactme" id="contactme">
<form class="form-horizontal" method="post" name="myemailform" action="form-to-email.php">
<fieldset>
<h2>Let's Create Together</h2>
<hr class="small">
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="name" placeholder="Name">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">E-Mail</label>
<div class="col-lg-10">
<input type="email" class="form-control" name="visitor_email" placeholder="E-Mail">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="8" name="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" name="submit" id="submitbutton" class="btn btn-primary mainButton">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
And here is the separate .php code. This is on a different file.
<?php
if (!isset ($_POST ['submit']))
{
//This page should not be accessed directly. Need to submist the form.
echo "error, you need to submit the form!";
}
$name = $_POST ['name'];
$visitor_email = $_POST ['visitor_email'];
$message = $_POST ['message'];
//Validation
if(empty ($name)||empty ($visitor_email))
{
echo "Name and email are mandatory!";
exit();
}
else {
echo "Submit Successful!";
header("Location: index.html");
exit();
}
$email_from = 'info@joshkelley.video'; //<== Put your email address here
$email_subject = "New Form Submission";
$email_body = "You have received a new message from the user $name.\n".
"E-mail Address: $visitor_email\n".
"Here is the message: $message\n".
$to = "brandontetrick@gmail.com"; // <==Put your email address here
$headers = "From: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done
?>
Any help or tips out be great. Thanks!
回答1:
The header() function will not work if you output ANYTHING even a space before it. But you could store your success message to a session variable.
First, you will have to rename your index.html into index.php
Add a session_start() function to the very top (again, never outpuyt anything before) in index.php and in your form script (anywhere where you work with $_SESSION variables):
<?php
session_start();
//....
Then change
echo "Submit Successful!";
header("Location: index.html");
exit();
for this
$_SESSION['success_msg'] = "Submit Successful!";
header("Location: index.html");
exit();
And in your index.php
<?php
session_start();
if(isset($_SESSION['success_msg'])) {
echo $_SESSION['success_msg'];
unset($_SESSION['success_msg']); // unset this after outputting, else it will keep showing
}
来源:https://stackoverflow.com/questions/43428332/submit-form-redirect-back-to-index-html