我正在将值从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" . "rn";
$headers .= "Content-type:text/html;charset=UTF-8" . "rn";
$headers .= "X-Priority: 1 (Highest)n";
$headers .= "X-MSMail-Priority: Highn";
$headers .= "Importance: Highn";
// More headers
$headers .= 'From: <info@example.com>' . "rn";
mail($to,$subject,$message,$headers);
}
}
?>
您正在调用一个不存在的类
$('.activate').click(function() {
你需要将其更改为id
$('#activate<?php echo $id; ?>').click(function() {
或者使用适当的类名
$('.onoffswitch-checkbox activate').click(function() {
mysql_real_eescape_string需要一个连接,如果没有它,您的值将被设置为NULL或空字符串。
您还可以使用PHPMailer脚本。
只需从下载PHPMailer文件https://github.com/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';
}
这会调用不存在的class="activate"
:
$('.activate').click(function() {
由于您的class
是onoffswitch-checkbox activate"
,您可以使用:
$('.onoffswitch-checkbox activate').click(function() {
或者,使用此调用id="activate"
:
$('#activate').click(function() {
我自己找到了答案。
<?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" . "rn";
$headers .= "Content-type:text/html;charset=UTF-8" . "rn";
$headers .= "X-Priority: 1 (Highest)n";
$headers .= "X-MSMail-Priority: Highn";
$headers .= "Importance: Highn";
// More headers
$headers .= 'From: <info@servername.com>' . "rn";
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>' . "rn";