duanji7881 2013-03-16 17:30
浏览 14
已采纳

检查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
});
  • 写回答

1条回答 默认 最新

  • duandan1995 2013-03-16 17:37
    关注
    <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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线