1

I was trying to to make a contact form working with jQuery AJAX and PHP. My HTML is as follows

<form action="#" id="contactForm" method="post" name="contactForm">
<fieldset>
    <div class="col-sm-12">
        <input id="contactName" name="contactName" placeholder="Your Name*" type="text" value="" required>
    </div>

    <!-- Name Field [ END ] -->

    <div class="col-sm-12">
        <input id="contactEmail" name="contactEmail" placeholder="Your Email*" type="email" value="" required>
    </div>

    <!-- Email Field [ END ] -->

    <div class="col-xs-12">
        <textarea cols="5" id="contactMessage" name="contactMessage" placeholder="Your Message....*" required></textarea>
    </div>

    <!-- Message Field [ END ] -->

    <div class="col-xs-12">
        <button class="submit">SEND</button>
    </div>

    <!-- Submit Button [ END ] -->

    <div class="error col-xs-12">
        <h3>Sorry! Your message was not sent.</h3>
    </div>

    <!-- Error Message [ END ] -->

    <div class="success col-xs-12">
        <h3>Success! Your message was sent.</h3>
    </div>

    <!-- Seccess Message [ END ] -->

</fieldset>

while jQuery code is as follows

$('form#contactForm').submit(function () {

    var url = $('#contactForm').attr('action'),
        type = $('#contactForm').attr('method'),

        contactName = $('#contactForm #name').val(),
        contactEmail = $('#contactForm #email').val(),
        contactMessage = $('#contactForm #message').val(),
        data = {
            name: contactName,
            email: contactEmail,
            message: contactMessage
        };

    $.ajax({
        type: type,
        url: url,
        data: data,
        success: function () {
            $('.success').fadeIn();
            $('.error').fadeOut();
        },
        error: function () {
            $('.error').fadeIn();
            $('.success').fadeOut();
        }
    });
    return false;
});

what could be possible PHP code to make this form working. I am using following code

 <?php

    if ( isset( $_POST['name'], $_POST['email'], $_POST['message'] ) ) {

        $to = 'mail@example.com'; //my email address here
        $subject = 'Message From :' . $_POST['name'];
        $message = $_POST['message'];
        $headers = 'From :' . $_POST['email'].'\r\n';

        mail($to,$subject,$message,$headers);

    }
?>

but it is not working. Success message shows but mail do not received. I am not familiar with PHP very well.

Please Help....

abpatil
  • 916
  • 16
  • 20
Nafees Anwar
  • 6,324
  • 2
  • 23
  • 42
  • can you try to send an email by inputting manual values for your php parts? I just wanna check if your mail mechanism is functioning well in the first place. – Sina Aug 21 '15 at 08:43
  • I tried but did not got any mail – Nafees Anwar Aug 21 '15 at 08:46
  • from where you are sending mail? from ur localhost or live server? – Vishnu Prasad Aug 21 '15 at 08:47
  • Then it is something up with your PHP mail mechanism. Are your server configs set properly? – Sina Aug 21 '15 at 08:47
  • Can you check your PHP logs to see if there are any errors being thrown – jolyonruss Aug 21 '15 at 08:49
  • I am using a free web hosting to test this. 000webhost – Nafees Anwar Aug 21 '15 at 08:52
  • Is another test mail working on it? – Vishnu Prasad Aug 21 '15 at 08:55
  • I did not checked but i think it should work – Nafees Anwar Aug 21 '15 at 08:56
  • @NafeesAnwar Check out the ans.. I have corrected your code and added it.. Its working ..checked..and mail received. – Vishnu Prasad Aug 21 '15 at 09:06
  • **Warning:** Your code is highly vulnerable to being hacked. You need to sanitise the `$_POST` input before passing it into the `mail()` function, otherwise, for example, an attacker could include `\r\n` in his email address, and add his own headers or even body content to your email. To avoid this kind of thing, the best solution is to use a decent third party library for mail rather than the built-in `mail()` function. I suggest you try either [phpMailer](https://github.com/PHPMailer/PHPMailer) or [Swiftmailer](https://github.com/swiftmailer/swiftmailer). – Simba Aug 21 '15 at 09:21

3 Answers3

0

Your sucess message shows becasue the php-page was called successfully, this does not indicate that the mail was sent successfully. You should check this by returning the mail-functions response:

$response = mail($to,$subject,$message,$headers);
echo $response;

This will return true or false. Also you will have to check that the mail is being recieved correctly, it could end up in spam, or be rejected by your mailserver for various reasons. There are some additions you can do to the mail-header if this is the case. Here are some pointers on how to get the mail through

Community
  • 1
  • 1
BobbyTables
  • 4,481
  • 1
  • 31
  • 39
0

Try adding

method: "POST",

In your $.ajax call, by default $.ajax uses GET requests

Lauromine
  • 1,453
  • 9
  • 19
0

This will Work for you for you

$('form#contactForm').submit(function () {

    var url = $('#contactForm').attr('action');
        var type = $('#contactForm').attr('method');

        var contactName = $('#contactName').val(); 
        var contactEmail = $('#contactEmail').val(); 
         var contactMessage = $('#contactMessage').val();
        data = {
            name: contactName,
            email: contactEmail,
            message: contactMessage
        };console.log(data);

    $.ajax({
        type: type,
        url: url,
        data: data,
        success: function () {
            $('.success').fadeIn();
            $('.error').fadeOut();
        },
        error: function () {
            $('.error').fadeIn();
            $('.success').fadeOut();
        }
    });
    return false;
});

Your PHP Code becomes

<?php
if ( isset( $_POST['name'], $_POST['email'], $_POST['message'] ) ) {

    $to = $_POST['email']; //my email address here
    $subject = 'Message From :' . $_POST['name'];
    $message = $_POST['message'];
    $headers = 'From :' . $_POST['email'].'\r\n';

    mail($to,$subject,$message,$headers);

}
        echo 'sucess';
?> 

And change here too

<form action="===GIVE YOUR FILE NAME CONTAINING PHP mail() CONTAINS====" id="contactForm" method="post" name="contactForm">

And You should confirm that your server has enabled with mail() function

Try this and let me know..

Vishnu Prasad
  • 729
  • 1
  • 11
  • 28