1

I am sending value from ajax to php to manipulate database values. Also i am trying to run mail() after posting value. But only manipulation in the database is happening. Please help. This is my form: 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>

This is my ajax code:

<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>

My code to update db and send mail:

<?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); 
} 
}
?>
Varuni N R
  • 802
  • 3
  • 11
  • 31

4 Answers4

1

You are calling a class which doesnt exist

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

You need to change it to an id

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

Or use the appropriate class name

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

mysql_real_escape_string requires a connection,without it your values will be set to NULL or empty string.

Mihai
  • 26,325
  • 7
  • 66
  • 81
1

You can also user PHPMailer script.

Just download PHPMailer files from https://github.com/PHPMailer/PHPMailer and include in your project.

In your ajax php file just include PHPMailerAutoload.php file and pass mail sending parameter as below.

<?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';
}
vijay rami
  • 535
  • 4
  • 19
0

This calls class="activate" which does not exist:

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

As your class is onoffswitch-checkbox activate", you can use:

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

Alternatively, use this to call id="activate":

$('#activate').click(function() {
Panda
  • 6,955
  • 6
  • 40
  • 55
0

I found the answer myself.

<?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 }
}
}
?>

Below header solved my problem. Also If condition.

$headers .= 'From: <info@servername.com>' . "\r\n";
Varuni N R
  • 802
  • 3
  • 11
  • 31