1

I'm trying to send a simple email from a form via jquery/AJAX. Any idea why this isn't working?

HTML:

<input id="referral-name" name="referral-name" type="text" placeholder="Your Name (First, Last)">
<input id="referral-email" name="referral-email" type="email" placeholder="Your Email">
<input id="friend-email" name="friend-email" type="email" placeholder="Your Friend's Email">
<button id="submit-referral">Send Referral</button>

Javascript:

$("#submit-referral").click(function () {

    var data = {
        referral-email: $("#referral-email").val(),
        referral-name: $("#referral-name").val(),
        friend-email: $("#friend-email").val()
    };

    $.ajax({
        type: "POST",
        url: "referral-email.php",
        data: data,
        success: function () {
            alert('Referral Sent');
        }
    });
});

PHP (named referral-email.php at the root of my folder):

<?php
if($_POST){
    $referral_name = $_POST['referral-name'];
    $referral_email = $_POST['referral-email'];
    $friend_email = $_POST['friend-email'];

//send email
    mail("my_email@email.com", "Referral Submission by ".$referral_name, $referral_name." (".$referral_email.") sent ".$friend_email." a referral.");
}
?>
user1661677
  • 1,252
  • 5
  • 17
  • 32
  • Does PHP produce any sort of error? – Mike Nov 08 '15 at 01:03
  • @Mike, I'm not exactly sure how to check for that. Any assistance? I'd be happy to check. – user1661677 Nov 08 '15 at 01:04
  • First, make sure [errors are being outputted](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). Then you need to look at the actual ajax response. This is different for every browser, but for Chrome you push F12, then go to the network tab and click on the request in the list. Then I think it's called "response" or something along those lines. If PHP is sending some sort of error, it should hopefully be there. – Mike Nov 08 '15 at 01:08

1 Answers1

0

Found the answer--

Adding quotations around my data array variables did the trick:

var data = {
        'referral-email': $("#referral-email").val(),
        'referral-name': $("#referral-name").val(),
        'friend-email': $("#friend-email").val()
    };
user1661677
  • 1,252
  • 5
  • 17
  • 32