ajax帖子和php邮件功能

时间:2016-04-10 09:20:19

标签: php jquery ajax email

我从ajax发送值到php来操纵数据库值。我也试图在发布值后运行mail()。但只有数据库中的操作才会发生。请帮忙。  这是我的表格:profile.php

    <form method="post" name="form">
        <table class="table v-middle display" id="table">
                        <thead>
                          <tr>
                         <th >Sl.no</th>
                         <th >Owner Name</th>
            <th >Contact Number</th>
            <th>Email </th>
            <th data-title="Action">Action</th>
                          </tr>
                        </thead>
     <tbody id="responsive-table-body">
<?php 
$owner=mysql_query("select id,tmp_id,name,phone,email,activate from pgowner where active='1'");
if(mysql_num_rows($owner)){
$i=0;
while($owner1=mysql_fetch_array($owner)){
$id=$owner1['tmp_id'];
?>
                          <tr>
<td><span class="label label-default"><?php echo ++$i; ?></span></td>
<td>
<a href='viewprofile?id=<?php echo $id; ?>' target='_blank' style='color:blue;text-decoration:underline;'><?php echo $owner1['name']; ?></a>
<input type="hidden" name="ownerid" value="<?php echo $owner1['tmp_id']; ?>" id="ownerid" />
</td>
<td class="phone">
<?php echo $owner1['phone']; 
?>
</td>
<td class="email">
<?php echo $owner1['email']; 
?>
</td>
<td>
<div class="onoffswitch">
<input type="checkbox" name="activate[]" class="onoffswitch-checkbox activate" id="activate<?php echo $id; ?>"
<?php
$query3=mysql_query("select tmp_id,activate from pgowner where tmp_id='$id'");
$query4=mysql_fetch_array($query3);
if($query4['activate']=="1")
{
echo "checked";
}
?>>
<label class="onoffswitch-label" for="activate<?php echo $id; ?>">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
</td>
</tr>
<?php }  }
?>                
</tbody>
</table>          
</form>

这是我的ajax代码:

<script type="text/javascript">

$(document).ready(function() {
  $('.activate').click(function() {
   var ownerid = $(this).closest('tr').find('input[name="ownerid"]').val();
   var email =$(this).closest('tr').find('td.email').text().replace(/\s+/g, " ");
   var phone =$(this).closest('tr').find('td.phone').text().replace(/\s+/g, " ");
   var a = this.checked ? 1 : 0;

   alert(email);
   alert( phone);

    $.ajax({
      type: "POST",
      url: "profileactivation",
      data: {
        value: a,
        ownerid: ownerid,
        email : email,
        phone : phone
      },
      success: function(html) {
       alert('Successfully Done');       
        },
      error : function(html){
      alert("Error occured !!");
      }
    });
  });
});
</script>

我更新数据库和发送邮件的代码:

<?php
if($_POST){
if(mysql_real_escape_string($_POST['value']=='1')){
$ownerid=mysql_real_escape_string($_POST['ownerid']);
$email=mysql_real_escape_string($_POST['email']);
$phone=mysql_real_escape_string($_POST['phone']);

$activate=mysql_query("update pgowner set activate='1' where tmp_id='$ownerid'");

$to=$email;
$subject = "Profile Activation Mail";
$message = "
<html>
<head>
<p>Activated</p>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";

// More headers
$headers .= 'From: <info@example.com>' . "\r\n";

mail($to,$subject,$message,$headers); 
} 
}
?>

4 个答案:

答案 0 :(得分:1)

您正在调用一个不存在的类

$('.activate').click(function() {

您需要将其更改为ID

$('#activate<?php echo $id; ?>').click(function() {

或使用适当的班级名称

$('.onoffswitch-checkbox activate').click(function() {

mysql_real_escape_string需要连接,没有它,您的值将被设置为NULL或空字符串。

答案 1 :(得分:1)

您也可以使用PHPMailer脚本。

只需从https://github.com/PHPMailer/PHPMailer下载PHPMailer文件并包含在您的项目中。

在你的ajax php文件中只包含PHPMailerAutoload.php文件并传递邮件发送参数如下。

<?php
require_once('../PHPMailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;// debugging: 1 = errors and messages, 2 = messages only                               // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'youremail.com';                 // SMTP username
$mail->Password = '*************';                           // SMTP password
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail  // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;           // or 587                         // TCP port to connect to
$mail->setFrom('youremail@gmail.com', 'My name');
$mail->addAddress($to, $ownername);     // Add a recipient
//$mail->addAddress('ellen@example.com');               // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
//$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

答案 2 :(得分:0)

这会调用不存在的class="activate"

$('.activate').click(function() {

由于classonoffswitch-checkbox activate",您可以使用:

$('.onoffswitch-checkbox activate').click(function() {

或者,使用此方法拨打id="activate"

$('#activate').click(function() {

答案 3 :(得分:0)

我自己找到了答案。

<?php
if(isset($_POST['value'])){
if(mysql_real_escape_string($_POST['value']=='1')){
$ownerid=mysql_real_escape_string($_POST['ownerid']);
$email=mysql_real_escape_string($_POST['email']);
$phone=mysql_real_escape_string($_POST['phone']);
$activate=mysql_query("update pgowner set activate='1' where tmp_id='$ownerid'");
$reactivatepg=mysql_query("update pg_details set approved='1' where owner='$ownerid' and approved='0' and publish='1'");

$to=$email;
$subject = "Profile Activation Mail";
$message = "
<html>
<head>
<p>Dear user,</p>
<p>Thank you for registering!</p>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";

// More headers
$headers .= 'From: <info@servername.com>' . "\r\n";

if(mail($to,$subject,$message,$headers)){ ?>
<p style="display:none;">Sent</p>
<?php 
} else { ?>
<p style="display:none;">Unsent</p>
<?php }
}
}
?>

下面的标题解决了我的问题。如果条件。

$headers .= 'From: <info@servername.com>' . "\r\n";