2016-04-10 30 views
1

我發送值從ajax到php來操縱數據庫值。另外我正在嘗試發佈值後運行郵件()。但只有數據庫中的操作正在發生。請幫忙。 這是我的形式:profile.phpajax後和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: <[email protected]>' . "\r\n"; 

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

回答

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: <[email protected]>' . "\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: <[email protected]>' . "\r\n"; 
1

要調用類不存在

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

您需要將其更改爲一個id

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

或者用相應的類名

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

mysql_real_escape_string需要一個連接,沒有它你的價值觀將被設置爲null或空字符串。

+0

」---->這是類激活 –

+0

好的,如果這是問題,那麼$ activate = mysql_query(「update pgowner set activate ='1',其中tmp_id ='$ ownerid'」);不應該工作。但這個作品 –

+0

只有郵件功能不能正常工作 –

0

這就要求class="activate"不存在:

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

當你的classonoffswitch-checkbox activate",你可以使用:

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

另外,使用這種調用id="activate"

$('#activate').click(function() { 
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('[email protected]', 'My name'); 
$mail->addAddress($to, $ownername);  // Add a recipient 
//$mail->addAddress('[email protected]');    // Name is optional 
//$mail->addReplyTo('[email protected]', 'Information'); 
//$mail->addCC('[email protected]'); 
//$mail->addBCC('[email protected]'); 
//$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'; 
}