0
votes

I am trying to send an email to user account using php mail() function.The mail is sent successfully but the issue is that it is sending me blank emails with no values in them! The code for the contact page that sends the email is as follows:-

<form  class="contact-form"  method="POST" action="sendemail.php">
                  <div class="row-fluid">
                    <div class="span5">
                        <label>First Name</label>
                        <input type="text" class="input-block-level" required="required" placeholder="Your First Name" name="name">
                        <label>Last Name</label>
                        <input type="text" class="input-block-level" required="required" placeholder="Your Last Name" name="lname">
                        <label>Email Address</label>
                        <input type="text" class="input-block-level" required="required" placeholder="Your email address" name="email">
                    </div>
                    <div class="span7">
                        <label>Message</label>
                        <textarea name="message" id="message" required="required" class="input-block-level" rows="8" name="message"></textarea>
                    </div>

                </div>
                <input type="submit" class="btn btn-primary btn-large pull-right" value="Send Message" />
                <p> </p>

            </form>

and sendemail.php page code is as follows:

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Email sent!'
    );

    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $subject = "An enquiry sir"; 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = $email;
    $email_to = '[email protected]';

    echo $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

    echo json_encode($status);
    die;
   ?>  

Why is the output I get blank in my email id, for example:

Name:

Email:

Subject:

Message:

P.N: I am using here a nova template theme.

The form is being submitted via AJAX using the following JavaScript:

var form = $('.contact-form');
form.submit(function () {
  $this = $(this);
  $.post($(this).attr('action'), function(data) {
    $this.prev().text(data.message).fadeIn().delay(3000).fade‌​Out(); 
  },'json');
  return false; 
});
2
The form gets posted with empty inputs? Try isset($_POST['name']) etc...AbraCadaver
done any basic debugging, like var_dump($_POST) to see what PHP's receiving? Plus, you have an lname field, but never bother handling that in PHP.Marc B
have you forgot to paste some code?? where is the subject field that you are retrieving? have you tried using die() with your values here..??RohitS
Your ajax javascript must not be sending the form data correctly, here is a demo of your code running correctly using regular html form submission (not ajax): public-klebestift.c9users.io . You need to examine the ajax request in the network tab of developer tools and see what is being sent.Luke
This is what you need to be looking for when we say examine the ajax request in the network tab of developer tools: screencast.com/t/kObKaEms4R but the entry you are looking for in the "Name" column should say "sendemail.php"Luke

2 Answers

0
votes

the form submission code is not submitting the form data. Here is the code you provided:

var form = $('.contact-form');
form.submit(function () {
  $this = $(this);
  $.post($(this).attr('action'), function(data) {
    $this.prev().text(data.message).fadeIn().delay(3000).fade‌​Out(); 
  },'json');
  return false; 
});

and this is what it should be:

var form = $('.contact-form');
form.submit(function () {
  $this = $(this);
  $.post($(this).attr('action'), $(this).serialize(), function(data) {
    $this.prev().text(data.message).fadeIn().delay(3000).fade‌​Out(); 
  },'json');
  return false; 
});
-3
votes

Remove the echo from the line that defines $body From this...

   echo $body = 'Name: ' . $name . "\n\n" . 'Email

To this...

   $body = 'Name: ' . $name . "\n\n" . 'Email