Vishal Kumar
Vishal Kumar

Reputation: 4617

PHP script sending mails but not showing form data

I am trying to send mail through a contact form. The script is executed and send mail to my email but couldn't capture the data... it's showing blank.

Here is my HTML code:

 <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" class="form-control" required="required">
                        </div>
                        <div class="form-group">
                            <label>Email *</label>
                            <input type="email" name="email" class="form-control" required="required">
                        </div>
                        <div class="form-group">
                            <label>Phone</label>
                            <input type="number" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>Company Name</label>
                            <input type="text" class="form-control">
                        </div>
                    </div>
                    <div class="col-sm-5">
                        <div class="form-group">
                            <label>Subject *</label>
                            <input type="text" name="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="btn btn-primary btn-lg" required="required">Submit Message</button>
                        </div>
                    </div>
                </form>

And here is the complete PHP script

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

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

    $email_from = $email;
    $email_to = '[email protected]';//replace with your email

    $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;

Actually I am using a HTML template.. and it was all there...I tried many ways to modify it but it's not capturing the form data.

Any suggestions...kindly check.

Upvotes: 0

Views: 2351

Answers (6)

Jefferson
Jefferson

Reputation: 1

Solution:

1) open file main.js in /js

2) find //contact form

3) substitute the code for:

jQuery(function($) {'use strict',

$('.contact-form').submit(function () {'use strict',
    $this = $(this);
    $.post("sendemail.php", $(".contact-form").serialize(),function(result){
        if(result.type == 'success'){
            $this.prev().text(result.message).fadeIn().delay(3000).fadeOut();
        }
    });
    return false;
});

});

4) test! :D

Upvotes: 0

Vishal Kumar
Vishal Kumar

Reputation: 4617

None of the above things worked for me. There was just a problem in underlying javascript... it was not able to get the data, serializing the form worked well for me

// Contact form

var form = $('#main-contact-form');
var data = $form.serialize()
form.submit(function(event){
    event.preventDefault();
    var form_status = $('<div class="form_status"></div>');
    $.ajax({
        type: "POST",
        dataType: "json",
        data: data,
        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();
    });
});

Upvotes: 0

Ranjeet Singh
Ranjeet Singh

Reputation: 702

Step to solving errors:

1) Check these variables are storing data:

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

by echo one by one.

2) Then pass them to $body variable.

3) Use proper header with content type and from and reply tags.

Try this one like:

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers`enter code here`
$headers .= 'To:' . "\r\n";
$headers .= 'From: Admin<[email protected]>' . "\r\n";
// Mail it
mail($to, $subject, $body, $headers);     

Upvotes: 0

arif_suhail_123
arif_suhail_123

Reputation: 2509

Note Read the comment in the answer. in there i explain, why you were able to sent mail. and not the other value

Like Change these line. Note use input type instead of button

<button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>

To these

<input type="submit" name="submit" class="btn btn-primary btn-lg" required="required">

Comment You are able to sent the mail cause you hardcoded it in your script

Like on this line

$email_to = '[email protected]';//replace with your email

Your other value depend on your form. These value. You are not getting these value through your form.

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

Comment Two dont use @ in production. as it supress the error. And error message are important during production.

Comment three Always sanitaize or validate user input. for security purpose.

Upvotes: 1

Nikhil Agarwal
Nikhil Agarwal

Reputation: 412

  1. The "@" symbol prior to a function in php will suppress any error messages. It is best to not use those until your application is ready for deployment. I recommending removing those until everything is working as you would like it.

  2. I recommend validating user input prior to processing it. There are several scripts online that you can use for the same.

  3. To fix your current issue use: $success = @mail($email_to, $subject, $message, 'From: <'.$email_from.'>');

Upvotes: 1

John Conde
John Conde

Reputation: 219814

Your content is in a variable called $message. You pass one called $body to mail(). If you had error reporting turned on php would have reported this to you.

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

FYI, using @ to suppress errors is a bad practice. It only makes debugging more difficult and is not the proper way to handle potential errors.

Upvotes: 1

Related Questions