I am trying to make a simple form that only has a message box and a submit button.
I took a contact form I am using on another page and just removed all fields I didn’t need. The form “appears” to submit but I never receive it in my email. I am sure I have made a simple mistake in the code.
Heres what I am using:
index.php (header):
<?php
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
//If the form is submitted
if(isset($_POST['submit'])) {
// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter a message!';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
// upon no failure errors let's email now!
if(!isset($hasError)) {
$emailTo = 'me@gmail.com';
$subject = 'Submitted message on Site.ca from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Comments: $comments";
$headers = 'From: ' .' <'.$emailTo.'>' . "\\r\
" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
// set our boolean completion value to TRUE
$emailSent = true;
}
}
?>
index.php (body):
<div id="actform">
<h3><img src="./_layout/images/bg-checklist.png" />Got an idea? Let us know below</h3>
<form action="index.php" method="post">
<textarea name="comments" class="actform" placeholder="Message:"></textarea>
<input type="submit" value="Submit" name="submit" class="subname" />
</form>
<div class="spacer"></div>
</div>
Can anyone spot what I am doing wrong? I need the form to reload the page on submit, which it currently does.
if you refresh the page after a submit, that would trigger the form submission again with previous data, so the email would send again.
it’s the default functionality of form submission.
I tried the following to see if it would solve it:
<?php
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
// process form data
if($emailSent) {
// there must be no output of any kind before this line:
header('Location http://www.site.ca/index.php?success=1');
exit;
}
//If the form is submitted
if(isset($_POST['submit'])) {
if(isset($_POST['submit']) && !$emailSent)
// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter a message!';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
// upon no failure errors let's email now!
if(!isset($hasError)) {
$emailTo = 'me@gmail.com';
$replyEmail = 'me@gmail.com';
$subject = 'Submitted idea on site.ca';
//$sendCopy = trim($_POST['sendCopy']);
$body = "Comments: $comments";
$headers = 'From: ' .' <'.$emailTo.'>' . "\\r\
" . 'Reply-To: ' . $replyEmail;
mail($emailTo, $subject, $body, $headers);
// set our boolean completion value to TRUE
$emailSent = true;
}
}
?>
But it doesn’t redirect to index.php?success=1 and still double sends on refresh.