简体   繁体   中英

On link click download file and run script to send mail

From last 3 days i am trying to solve this simple problem but no success. What I am trying to do is that on link button click I have to download a file and send notification by mail

$path is path of file to be downloaded

   echo '<a href="'.$path.'" class="links" seq="'. $id .'" >Download</a>';

jquery to call sendMail.php onclick

  $(".links").click(function(){
      var s=($(this).attr('seq'));
     alert(s);
     $.ajax({
             type:'POST',
             url:'sendMail.php',
             data:{s:s},
             success:function(result){

                  alert(result);
                  }

         }) 

     }); 

The problem is that when I click on the download link file gets downloaded but send mail script not working. Is there anything like download and send mail both at a time on possible on link click in php . Thanks in advanced..

sendMail.php script

   $q=$_POST['s'];
   echo "this is q value".$q;
   $sqlid="SELECT * FROM files_table WHERE id = '".$q."'";
   $result = mysqli_query($conn,$sqlid);
   $row=mysqli_fetch_array($result);
   //collect data from table
   $id=$row['id'];
   $email =$row['user_email'];
   $message=$row['message'];
   $file_type=$row['file_type'];
   $download=$row['file_name'];
   $time=$row['time'];
  $file_name=$row['file_name'];

   $to=$email;
   $from = "support@print.com"; // sender
   $subject = $id.'   '.$message;
   $message = " 

  messsage text
    ";

// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail($to,$subject,$message,"From: $from\n");
echo "Mail Sent";  

try this - first the php action is called after the mail is sending out you will be redirected to the download link

$(".links").click(function (e) {
    e.preventDefault();
    var self = $(this);
    var s = self.attr('seq');
    var href = self.attr('href');
    alert(s);
    $.ajax({
        type: 'POST',
        url: 'sendMail.php',
        data: {s: s},
        success: function (result) {
            window.location.href = href;
        }
    })
});

You need to capture the click event and prevent the page from loading te link. It reloads your window so your JS will not be triggerd. After preventing from triggering the link do trigger the click again

Try this:

  $(".links").click(function(event){
     event.preventDefault();
      var s=($(this).attr('seq'));
     alert(s);
     $.ajax({
             type:'POST',
             url:'sendMail.php',
             data:{s:s},
             success:function(result){

                  alert("success");

                  }

         }); 
       window.location.href = $(this).attr("href");
     }); 

EDIT

I think your JS is working correctly if you use the aqbove sample, lets try to debug your PHP code:

What you can do to test you problem:

  1. verify an id got send with your POST

  2. verify the DB select works as its supossed to, and returns an acutual result.

try something like this: $result = mysqli_query($conn,$sqlid);

if(mysql_num_rows($result) == 0) 
{ 
    echo "no Results";
}
else{
    // only one result so we dont do a while loop here
    $row = mysqli_fetch_assoc($result);

    $id=$row['id'];
    $email =$row['user_email'];
    $message=$row['message'];
    $file_type=$row['file_type'];
    $download=$row['file_name'];   // Two times assign "file_name" ??
    $time=$row['time'];
    $file_name=$row['file_name'];

}
  1. Switch mysqli_fetch_array($result); for mysqli_fetch_assoc($result); it's faster, you dont use the numeric index mysqli_fetch_array returns. (see example above)

  2. Test the mailing as basic as it could be and then try to add stuff.

mail('yourmail@gmail.com', 'My Subject', $message);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM