SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Contact form

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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):

    Code:
    <?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\n" . 'Reply-To: ' . $email;
    
    		mail($emailTo, $subject, $body, $headers);
            
            // set our boolean completion value to TRUE
    		$emailSent = true;
    	}
    }
    ?>
    index.php (body):

    Code:
    <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.

  2. #2
    Non-Member
    Join Date
    Apr 2010
    Posts
    298
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did, I just changed it to that for posting here.

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

  4. #4
    SitePoint Member battambang_kid's Avatar
    Join Date
    Feb 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this

    PHP Code:
    //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\n" 'Reply-To: ' $replyEmail;

            
    mail($emailTo$subject$body$headers);
            
            
    // set our boolean completion value to TRUE
            
    $emailSent true;
        }
    }
    ?> 

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, that did it.

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

  6. #6
    SitePoint Member battambang_kid's Avatar
    Join Date
    Feb 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Member battambang_kid's Avatar
    Join Date
    Feb 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    redirect to a non submited page is one of the easiest ways to do it.

    or your can try this

    PHP Code:
    $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\n" 'Reply-To: ' $replyEmail;

            
    mail($emailTo$subject$body$headers);
            
            
    // set our boolean completion value to TRUE
            
    $emailSent true;
        }
    }
    ?> 
    PHP Code:
    <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>

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried the following to see if it would solve it:

    Code:
    <?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\n" . '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.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still sens a duplicate email. I may need to look at incorporated a session code somehow.

  10. #10
    SitePoint Member battambang_kid's Avatar
    Join Date
    Feb 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the $emailSent is not available on ur top code block

    PHP Code:
    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

    PHP Code:
    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

    PHP Code:
    $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

    Code:
    <input type="hidden" name="isSent" value="<?php echo ($emailSent)?1:0; ?>" />
    make sure u copied all the code.

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I copied the code you did earlier, but, on refresh I still get it sent twice.

  12. #12
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's what I have:

    Head:
    Code:
    <?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\n" . 'Reply-To: ' . $replyEmail;
    
            mail($emailTo, $subject, $body, $headers);
            
            // set our boolean completion value to TRUE
            $emailSent = true;
        }
    }
    ?>
    body:
    Code:
    <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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •