检查DB,返回消息,还发送电子邮件的功能

In this example I am using MySQL, which I know is deprecated, but this is just for learning purposes. I am also learning how to use PDO and right now just dont wanna mess up fue to my inexperience with PDO so I am using mysql right now.

Ok, so I have a jQuery AJAX function that submits a form data to a PHP function page. The PHP page then communicates with the Database and returns a result. All of this works so far.

A user fills out a form supplying their email address. The email is passed to teh PHP page and entered into the DB. If the user exists, a message is displayed telling them that they are already subscribed. If the do not exits, they are added and then a message tells them they have been successfully added. The GOOD NEWS is all of this works great!

Now, where I have an issue is that during that same callback function "dispAdd", I want to generate an automated welcome email to the user. No matter how I try to code the mail call though, I seem to get an error in the function. I am going to give you what I have right now but if someone can help that would be hugely appreciated.

Here is my callback function because all the other pieces work ok right now:

function dispAdd()             // Serves as callback function to jQuery/AJAX in contact.html
{
  $sql= "SELECT * FROM mailList WHERE email = '$email'";
  $result= mysql_query($sql) or die(mysql_error());
  $to = "rmailloux11@mail.bristol.mass.edu";
  $who = "ME";
  $message = "WOW";
  $subject = "TESTING";
  $message = $who . ', ' . $message;
  $headers = "From: rmailloux11@mail.bristol.mass.edu" . "
";

  if(mysql_num_rows($result) > 0)  // Checks to see if query returns any info for the calling function
  {
    mail($to,$subject,$message,$headers);
    while ( $row = mysql_fetch_assoc($result))
    return;
  }
}

Original CALL:

$('#contForm').submit(function() {
  var formData = $(this).serialize();            // Stores all form data in AJAX variable
  $.post('contact.php',formData,dispAdd);
  function dispAdd(result) {               // Callback function requests result of RESULT
    if (!result) {
      $('#main').html('<div>Your have been added to the mailing list</div>');
    } else {
      if ($('#fail').length==0) {
        $('#main').append('<div id="fail" style="color:red";>This email address is already subscribed to our mailing list</div>');
      }
    }
  }
  return false;    // Stops form from loading contact.php page
});
<script>
  $('#contForm').submit(function()
  {
    var formData = $(this).serialize();            // Stores all form data in AJAX variable
    $.post('contact.php',formData, function(data)
    {
      console.log(data);
      if(data)
      {
        $('#main').html('<div>You have been added to the mailing list</div>');
      }
      else
      {
        $('#main').append('<div id="fail" style="color:red";>This email address is already subscribed to our mailing list</div>');
      }
      console.log(data);
    });
    return false;    // Stops form from loading contact.php page
  });
</script>

Then, in contact.php you should put the below function:

function dispAdd()             // Serves as callback function to jQuery/AJAX in contact.html
{
  $sql= "SELECT * FROM mailList WHERE email = '$email'";
  $result= mysql_query($sql) or die(mysql_error());
  $to = "rmailloux11@mail.bristol.mass.edu";
  $who = "ME";
  $message = "WOW";
  $subject = "TESTING";
  $message = $who . ', ' . $message;
  $headers = "From: rmailloux11@mail.bristol.mass.edu" . "
";

  if(mysql_num_rows($result) > 0)   // Checks to see if query returns any info for the calling function
  {
    mail($to,$subject,$message,$headers);
    while ( $row = mysql_fetch_assoc($result))
    return;
  }
}

After the code on contact.php runs, have it call the dispAdd function and return the result of dispAdd to your Ajax request.