1

I have an ajax call that sends some data (first name, last name, email) to a php file which is supposed to email me. The data I'm sending exists, I've checked that. When I run it however I am getting a blank error. Am I missing something easy? Any advice?

$.ajax({
    type: 'POST',
    url: 'signup.php',
    data: { first_name: 'first_name', last_name: 'last_name', email_address: 'email_address'},
    contentType: 'application/json; charset=utf-8',
    async: false,
    success: (function() {
        alert("Successful");
    }),
    error: (function(data){

    })
});

In signup.php I have the following:

<?php

// Get User Information From AJAX Post
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];

//Set Email Information
$to = 'example@example.com';
$subject = 'New User';
$message = "New User\n$first_name\n$last_name\n$email_address";
$headers = 'From: example@example.com'. "\r\n";

//Send Email
mail($to, $subject, $message, $headers);

?>
user3147424
  • 3,022
  • 5
  • 19
  • 22

1 Answers1

2

Your problems are

  1. You're attempting to post JSON(but not succeeding)
  2. You're attempting to read posted form data though you attempted to post JSON.

I suggest not posting json, but normal url encoded form data, that way $_POST will be populated with the values sent.

If you pass an object in the data field it will be encoded for you so you wont have to doing 'first_name='+encodeURIComponent(first_name)+'&last_name='+encodeURIComponent(last_name)+'&email_address'+encodeURIComponent(email_address)

$.ajax({
    type: 'POST',
    url: '/signup.php',
    data: {'first_name': first_name, 'last_name': last_name, 'email_address': email_address},
    async: false,
    success: (function() {
        alert("Successful");
    }),
    error: (function(ts){
        alert(ts.responseText);
    })
});

Also in your php you try some string interpolation(putting variables in a string), but you use single quotes, you have to use double quotes.

$message = "New User\n$first_name\n$last_name\n$email_address";
Musa
  • 96,336
  • 17
  • 118
  • 137
  • The email sends, but it will not send my variables...even when I sent the variable to a hard coded value – user3147424 Jan 24 '14 at 22:28
  • your update works for hard coded data. I'm having problems getting my POST data though, when I alert in javascript right before my ajax call, my variables all have values...but not when I get to php..? – user3147424 Jan 25 '14 at 02:50
  • @user3147424 post what you have now – Musa Jan 25 '14 at 03:07
  • I've updated my code above...I receive a successful alert and an email. The email simply says New User with no other data – user3147424 Jan 25 '14 at 03:13
  • @user3147424 I forgot to take out the JSON content type, try it now. – Musa Jan 25 '14 at 03:14
  • works, but is displaying my $first_name as literally first_name...my email is New Campaign first_name last_name email_address – user3147424 Jan 25 '14 at 03:19