-3

I have a normal HTML form which sends to a sendemail.php script.

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Thank you for contact us. We will contact you as soon as possible.'
    );

    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $number = @trim(stripslashes($_POST['number']));
    $company = @trim(stripslashes($_POST['company']));    
    $subject = @trim(stripslashes($_POST['subject'])); 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = $email;
    $email_to = '';

    $body = 'Name: ' . $name . "\n\n" .'Number: ' . $number . "\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);

    echo $email;
    die;

    ?>

It is sending to the the specified email address but it is not displaying the Variables in the email. Would anyone be able to shed some light on where I am going wrong.

        <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
            <div class="col-sm-5 col-sm-offset-1">
                <div class="form-group">
                    <label>Name *</label>
                    <input type="text" name="name" id="name" class="form-control" required="required">
                </div>
                <div class="form-group">
                    <label>Email *</label>
                    <input type="email" name="email" id="email" class="form-control" required="required">
                </div>
                <div class="form-group">
                    <label>Phone</label>
                    <input type="number" name="number" id="number" class="form-control">
                </div>
                <div class="form-group">
                    <label>Company Name</label>
                    <input type="text" name="company" id="company" class="form-control">
                </div>                        
            </div>
            <div class="col-sm-5">
                <div class="form-group">
                    <label>Subject *</label>
                    <input type="text" name="subject" id="subject" class="form-control" required="required">
                </div>
                <div class="form-group">
                    <label>Message *</label>
                    <textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
                </div>                        
                <div class="form-group">
                    <button type="submit" name="submit" class="floatright btn btn-primary btn-lg" required="required">Submit Message</button>
                    <br><br><Br><br>    
                </div>
            </div>
        </form>

Edited with the form code above


var form = $('#main-contact-form');
form.submit(function(event){
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        url: $(this).attr('action'),

        beforeSend: function(){
            form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
        }
    }).done(function(data){
        form_status.html('<p class="text-success">' + data.message + '</p>').delay(3000).fadeOut();
    });
});

Added in the relevent JS above ......

1 Answers1

0
  1. Drop anything that's not necessary.
  2. Add debugging statements to check input and intermediate variables
  3. Enable debugging output and stop using those @ prefixes
  4. Run again and post output

So:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

var_dump($_POST);
$name = $_POST['name']; 
$email = $_POST['email']; 
$number = $_POST['number'];
$company = $_POST['company'];    
$subject = $_POST['subject']; 
$message = $_POST['message']; 

$email_from = $email;
$email_to = '';

$body = 'Name: ' . $name . "\n\n" .'Number: ' . $number . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
var_dump($body);

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

Update: Now that you are telling us that you send the email via an ajax request it's all clear. Ajax is sending a GET request to the server, which does not contain any data from the form. To my limited understanding of JQuery you should:

  1. add the data attribute to the ajax call and supply it with a json array of your form data
  2. add the method attribute to the ajax call and set it to POST

    var form = $('#main-contact-form'); form.submit(function(event){ event.preventDefault(); var form_status = $(''); $.ajax({ url: $(this).attr('action'), data: [/* your form data here */], method: 'POST', beforeSend: function(){ form.prepend( form_status.html('

    Email is sending...

    ').fadeIn() ); } }).done(function(data){ form_status.html('' + data.message + '').delay(3000).fadeOut(); }); });

I don't know JQuery well enough to know if there's a convenient way to encapsulate your form data into a json structure. But it's certainly possible to get your individual fields by their ids and get their values.

Kempeth
  • 1,756
  • 2
  • 18
  • 34
  • It returns as "undefined"...The email comes through but again with no variables. Just the plain text. – Springheeled Jack Aug 03 '17 at 12:21
  • when i rewrite the html form in really basic html it runs the var_dump array(6) { ["email"]=> string(12) "asd@jdjd.com" ["name"]=> string(6) "asdasa" ["number"]=> string(2) "ad" ["company"]=> string(3) "asd" ["subject"]=> string(3) "asd" ["message"]=> string(6) "asdasd" } string(76) "Name: asdasa Number: ad Email: asd@jdjd.com Subject: asd Message: asdasd" bool(true) {"type":"success","message":"Thank you for contacting us. We will contact you as soon as possible."} – Springheeled Jack Aug 03 '17 at 12:31
  • I have narrowed it down the form "id" not sure why this is effecting it though. Any thoughts? – Springheeled Jack Aug 03 '17 at 12:51
  • have added in the JS that corresponds – Springheeled Jack Aug 03 '17 at 12:56
  • @SpringheeledJack That you're using AJAX would have been extremely useful to know up front. See my updated answer. – Kempeth Aug 03 '17 at 13:19