0

For creating a .zip file for checked items with selectbox, i need a response back from the php that leads to the path the .zip file is stored.

This is my ajax call:

// AJAX for Checkbox download

$(document).on('click' , '.cb_down' , function() {      
    var checkboxes_down = [];               
    $('.rafcheckbox').each(function() {   
        if(this.checked) {              
             checkboxes_down.push($(this).val());                                
        }  
    });  
    checkboxes_down = checkboxes_down.toString(); 

   $.ajax({  
        url:"",                                 
        method:"POST",                  
        data:{ checkboxes_down:checkboxes_down },  
        success:function(response){
            window.location = response; // this should lead me to the zip file
        }
        //.........

My php:

// Multiple download (checkboxes)
if(isset($_POST["checkboxes_down"])) { 

   // create a tmp folder for the zip file
   $tmpfolder = $MainFolderName.'/tmpzip';
   if (!is_dir($tmpfolder)) {
     mkdir($tmpfolder, 0755, true);
   }

   $checkboxfiles = explode("," , $_POST["checkboxes_down"]); 
   $filename = "archive.zip";
   $filepath = $tmpfolder."/";

   foreach($checkboxfiles as $checkboxfile) {               
     Zip($checkboxfile, $tmpfolder."/archive.zip"); // Zip is a function that creates the .zip file
   }   

   // header come here

   echo $filepath . $filename; // the path to the .zip file

   exit; 

The .zip file is successful created. I checked it. The problem is: i do not get the response back from the php script. So i can not download the .zip file. What i am doing wrong?

! I changed the echo to 'zip file is created' but even that echo i do not receive as response back

mudraya
  • 99
  • 7
  • You should send response in json from server side. – Amit Rajput Feb 26 '19 at 09:43
  • 1
    "i do not get the response back from the php script" — The only thing I can think of which would cause that behaviour is if you did something (e.g. submit a form) which causes the browser to navigate away from the page with the JavaScript in it before the response got back from the server. There isn't enough code in your question to determine if that is happening though. – Quentin Feb 26 '19 at 09:45
  • Also check "console.log(response)" in client side. – Amit Rajput Feb 26 '19 at 09:45
  • 1
    Use chrome Inspector->Network to find out what your php script is actually doing. Add some more echo's in there as well just to debug. – Amos Fox Feb 26 '19 at 09:51
  • @Amos: i made an echo: `'zip file is created` in the php and even that echo i do not receive as response! – mudraya Feb 26 '19 at 10:01
  • "even that echo i do not receive as response" — What are you doing to determine this? – Quentin Feb 26 '19 at 10:11
  • Can you add "Zip()" method/function code with your question? – Amit Rajput Feb 26 '19 at 10:12
  • @Quentin in my php: `echo 'zip file is created'` and in my success function: `$('.echo').html(response);` . The div with class `echo` should contain the echo but it is not there – mudraya Feb 26 '19 at 10:24
  • @Amit `.zip` is successful created. that is not the problem. – mudraya Feb 26 '19 at 10:29
  • @mudraya —You should follow the advice you were given earlier — "Use chrome Inspector->Network to find out what your php script is actually doing." — look at the details of the request and the response. – Quentin Feb 26 '19 at 10:30
  • first of all, comment out your ZIP() call and add an echo in that for loop to print out the file details. lets start there and let us know what you get – Amos Fox Feb 26 '19 at 21:17

0 Answers0