Contact form

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.

thanks.

This may sound like an easy fix but…did you check this:

	$emailTo = 'me@gmail.com';

Did you change me@gmail.com to your own email?

I did, I just changed it to that for posting here.

Seems like it should work, but, there must be something I am missing.

try this

//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';
		$replyEmail = 'reply@email.com';
		$subject = 'Submitted message 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;
	}
}
?>

Perfect, that did it.

However, now, I get double the messages on a page refresh. Any ideas?

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.

redirect to a non submited page is one of the easiest ways to do it.

or your can try this


$emailSent = isset($_POST['isSent'])?$_POST['isSent']:FALSE;;
//If the form is submitted
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 = 'reply@email.com';
        $subject = 'Submitted message 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;
    }
}
?>


<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">
<input type="hidden" name="isSent" value="<?php echo ($emailSent)?1:0; ?>" />
<textarea name="comments" class="actform" placeholder="Message:"></textarea>
<input type="submit" value="Submit" name="submit" class="subname" />
</form>
<div class="spacer"></div>
</div>

Still sens a duplicate email. I may need to look at incorporated a session code somehow.

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.

the $emailSent is not available on ur top code block


if($emailSent) {
   // there must be no output of any kind before this line:
   header('Location http://www.site.ca/index.php?success=1');
   exit;
} 

so the code never got executed.

and the correct syntax for redirecting is


header("Location: http://google.com");

and u duplicate check the if(isset($_POST[‘submit’])) {

please check the code i posted earlier, on the top of the code i have added a checking if the form was once submitted


$emailSent = isset($_POST['isSent'])?$_POST['isSent']:FALSE;

and in the body i have inserted a hidden field to flag if the form was previously submitted


<input type="hidden" name="isSent" value="<?php echo ($emailSent)?1:0; ?>" />

make sure u copied all the code.

I copied the code you did earlier, but, on refresh I still get it sent twice.

Here’s what I have:

Head:

<?php
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
$emailSent = isset($_POST['isSent'])?$_POST['isSent']:FALSE;
//If the form is submitted
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 = 'reply@email.com';
        $subject = 'Submitted message 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;
    }
}
?>

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">
<input type="hidden" name="isSent" value="<?php echo ($emailSent)?1:0; ?>" />
<textarea name="comments" class="actform" placeholder="Message:"></textarea>
<input type="submit" value="Submit" name="submit" class="subname" />
</form>
<div class="spacer"></div>
</div>

I don’t think I missed anything.