Reputation: 21
I have searched on here and can't seem to find a satisfactory solution so am asking the question.
I have a one page website with a form that works with regards to sending to the correct email address, and displaying the right echoed result header; but when testing, every time I refresh the page the message re-sends, in addition to this I'd like the browser view port to display the contact form after the submit button is pressed so that the user can either see the form sent or error messages.
This is my code...
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'marc@**************.com';
$subject = 'New enquiry from website';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="emailSent">Thanks. Your message has been sent! We will be in touch shortly.</p>';
} else {
echo '<p class="errorMessage">Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p class="errorMessage">You answered the anti-spam question incorrectly. Please try again.</p>';
}
?>
<div class="large-6 medium-6 small-12 columns form-left">
<form class="footer-form" method="post" action="index.php">
<div class="row">
<div class="medium-9 medium-push-3 columns">
<label>
<input name="name" type="name" id="name" value="" placeholder="Your name" />
</label>
</div>
</div>
<div class="row">
<div class="medium-9 medium-push-3 columns">
<label>
<input name="email" type="email" id="email" value="" placeholder="Email Address" />
</label>
</div>
</div>
<div class="row">
<div class="medium-9 medium-push-3 columns">
<label>
<textarea name="message" id="message" placeholder="Your enquiry"></textarea>
</label>
</div>
</div>
<div class="row">
<div class="medium-9 medium-push-3 columns">
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" id="name" placeholder="Type Here">
</div>
</div>
<div class="row">
<div class="medium-9 medium-push-3 columns">
<button class="submit contact-button" id="name" name="submit" type="submit" value="Send email">Send</button>
</div>
</div>
</form>
</div>
I would really appreciate some help on this.
Thanks.
Marc
Upvotes: 1
Views: 1372
Reputation: 9
Insted of setting the 'Location' in the header function to 'index.php' just set it to the name of page you've created with the form in it, that should do it.
Upvotes: 0
Reputation: 620
Do you mean the browser asks if you'd like to resubmit the form after refresh? If so, it will always resubmit the information, what you can do, though, is place the php code in another file, tell the form to go the that file, and redirect back to the form page after the script is executed.
EDIT
Ok, have a page specifically for the form, let's call it form.php.
On your template page, or wherever you user interface is (for this example let's pretend it's index.php), create a form the same way you would, but instead of setting the "action" of the page to index.php, set it to form.php.
e.g. <form action="form.php" method="post">
This is all you have to do if you want to redirect the user to a different page for the form. Then they can find there way around your website.
Use this if you want to bring them back to index.php instantly
Still do the first, step, which was instead of setting the "action" of the page to index.php, set it to form.php.
e.g. <form action="form.php" method="post">
Now, when the user is on form.php, everything will happen pretty quickly, so make sure at the end of the script, you redirect them back to index.php using header('Location: index.php');
You also said you wanted an error or success method, no problem.
If you want the message to be dependant on if
statements within the form page, do this. On form.php, before you redirect them, create a $message variable and set it to whatever you want. Then back in your index.php, write a include_once('form.php');
which will allow you to use the $message
variable you created on form.php in your index.php.
The message should probably be $message = ""
before the user is redirected so nothing shows up until the form is submitted, but this solution should work fine, I hope this helps!
Upvotes: 2