1

As an mildly-intermediate web developer, I have never actually implemented a contact form until now. The problem is that I can't get the email to actually go through.

HTML:

<form action="php/handleFormSubmit.php" id="contact-form" role="form" method="POST">
    <div class="ajax-hidden">
        <div class="form-group wow fadeInUp">
            <label class="sr-only" for="c_name">Name</label>
            <input type="text" id="c_name" class="form-control" name="c_name" placeholder="Name">
        </div>
        <div class="form-group wow fadeInUp" data-wow-delay=".1s">
            <label class="sr-only" for="c_email">Email</label>
            <input type="email" id="c_email" class="form-control" name="c_email" placeholder="E-mail">
        </div>
        <div class="form-group wow fadeInUp" data-wow-delay=".2s">
            <textarea class="form-control" id="c_message" name="c_message" rows="7" placeholder="Message"></textarea>
        </div>
        <button type="submit" class="btn btn-lg btn-block wow fadeInUp" data-wow-delay=".3s">Send Message</button>
    </div>
    <div class="ajax-response"></div>
</form>

PHP:

<?php

if(isset($_POST['submit']))
{

  $name = $_POST['c_name'];
  $visitor_email = $_POST['c_email'];
  $message = $_POST['c_message'];

  $email_from = "email@email.com";

  $email_subject = "New Form submission";

  $email_body = "You have received a new message from the user $name.\n".
                            "Here is the message:\n $message".

  $to = "email@email.com";

  $headers = "From: $email_from \r\n";

  $headers .= "Reply-To: $visitor_email \r\n";

  mail($to,$email_subject,$email_body,$headers);

}

if(isset($_POST['c_name'])){

    $res['sendstatus'] = 1;
    $res['message'] = 'Form Submission Successful';
    echo json_encode($res);
}

?>

I know the if(isset($_POST['submit'])) gets rid of the annoying email when refreshing/landing but my submit does not go to my email.

Help? I appreciate it.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
NKReza
  • 11
  • 1
  • I'm not sure what your email software you have that PHP connects to but Have you tried appending a `\n\n` to the email message body to denote the end of message? – Mike -- No longer here Aug 01 '15 at 04:43
  • are you executing this to local system? Or to any server? – Alive to die - Anant Aug 01 '15 at 04:43
  • `mail($to,$email_subject,$email_body,$headers);` add $res : `$res = mail($to,$email_subject,$email_body,$headers);` then do a var dump to check if $res is true or not – zeflex Aug 01 '15 at 04:44
  • I am linking to gmail. It's funny because I receive an email when I didn't include the " if(isset($_POST['submit'])) { " in the beginning. I would repeatedly receive emails for either refreshing or landing on the page; however not when I actually submit. I'm on a server, host gator. My CSS is probably a little funky and I do have js linked to the form. – NKReza Aug 01 '15 at 04:45

4 Answers4

1

Check the first line, it says if $_POST['submit'] is set then do this{}. but your html form doesn't have that field.

ADD this field in your form:

<input type="hidden" name="submit">

Crunch Much
  • 1,537
  • 1
  • 11
  • 14
0

Code for sending Mails via Ajax

<?php
    if($_POST)
    {
        $to_email       = "er.shakun90@gmail.com"; //Recipient email, Replace with own email here

        //check if its an ajax request, exit if not
             if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

            $output = json_encode(array( //create JSON data
                'type'=>'error', 
                'text' => 'Sorry Request must be Ajax POST'
            ));
            die($output); //exit script outputting json data
             } 

        //Sanitize input data using PHP filter_var().
        $user_name      = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
        //$user_email       = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
        //$country_code = filter_var($_POST["country_code"], FILTER_SANITIZE_NUMBER_INT);
        $phone_number   = filter_var($_POST["user_phone"], FILTER_SANITIZE_NUMBER_INT);
        //$subject      = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
        $message        = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);

        //additional php validation
        if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error.
            $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
            die($output);
        }


        if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field
            $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
            die($output);
        }


        if(strlen($message)<3){ //check emtpy message
            $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
            die($output);
        }

              $user_email = 'contact@sanktik.net';

        //email body
        $message_body = $message."\r\n\r\n-".$user_name."\r\nPhone Number :". $phone_number ;

              $sunject = "New enquiry from wesbite";

        //proceed with PHP email.
        $headers = 'From: '.$user_name.'' . "\r\n" .
        'Reply-To: '.$user_email.'' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

        $send_mail = mail($to_email, $subject, $message_body);

        if(!$send_mail)
        {
            //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
            $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
            die($output);
        }else{
            $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your interest.Our Team Member will get in touch with you shortly'));
            die($output);
        }
    }
    ?>
Bugfixer
  • 2,547
  • 2
  • 26
  • 42
0

Change your button code to:

<button type="submit" name="Submit" class="btn btn-lg btn-block wow fadeInUp" data-wow-delay=".3s">Send Message</button>

and your php file first line to:

if(isset($_POST['Submit']))
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
0

Don't check if the button exists in the POST array. It makes sense if you click the button or submit the form via Enter press! I think by submitting via Enter press, the submit button doesn't exist!

Solution below is insensitive:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // sent  

Important things:

  • Check if form is real posted (see above)
  • Validate your input
  • Sanatize your input

After that, you can do your actions, e.g. mail or saving to database.

schellingerht
  • 5,726
  • 2
  • 28
  • 56