skip to Main Content

I want to generate a csv file with a dynamic filename which came from the values I got using an ajax jquery request. These values will be used as filename for my generated csv file. My program does not generate the csv file. What should I do for the csv file to be generated?

I am a newbie when it comes to php and especially AJAX and Jquery so I am still a bit confused on how an AJAX request works.

I’m using a free trial version of Sublime IDE and localhost for php. The program displays the contents of the csv file through an alert box but it doesn’t generate a csv file.

This is my jquery ajax request file:


 var selectedLanguage = $('#selectLanguage').val();
 var length;

 length = selectedLanguage.length;

 for (var i = 0; i < length; i++) 
    {
        var selectedLanguage = $('#selectLanguage').val();
        $.ajax({
            crossDomain: true,
            async: false,
            url: "downloadFile.php",
            type: "GET",
            data: {"seLang": selectedLanguage[i]},
                success: function(data) 
                { 
                    alert(data);
                    location.reload();
                }
        });

    }

This is my downloadFile.php code:

<?php
  $id="";

  if(isset($_GET['seLang'])){
   $id = $_GET['seLang'];

    $filename = "File_".$id.".csv"; 
    $f = fopen('php://memory', 'w');
    //set headers for column
    $fields = array('Content1', 'Content2', 'Content3','Content4');
    fputcsv($f, $fields, ',');

    fseek($f, 0);
    header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    header('Content-Disposition: attachment; filename="' . $filename . '";');
    fpassthru($f);


  }

  else
  {
    echo "<script type='text/javascript'>alert('Empty');</script>";

  }
?>

Edit 1:
Thank you Prvn and Sunday Johnson for trying to help. Although, I’ve found the actual problem. I’ve read from another post in this site that it is isn’t possible to download the generated csv file using AJAX. What else can I do for me to download a csv file?

2

Answers


  1. One comment about the Ajax code, why are you iterating over the value?

    About the php code, use this instead:

    <?php
      $id="";
    
      if(isset($_GET['seLang'])){
          $id = $_GET['seLang'];
    
          $filename = "File_".$id.".csv"; 
          $f = fopen($filename, 'w');
          //set headers for column
          $fields = array('Content1', 'Content2', 'Content3','Content4');
          fputcsv($f, $fields, ',');
          fclose($f);
      }
    
      else
      {
        echo "<script type='text/javascript'>alert('Empty');</script>";
    
      }
    

    ?>

    Login or Signup to reply.
  2. header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="sample.csv"');
    
    $fields = array('Content1', 'Content2', 'Content3','Content4');
    $fp = fopen('php://output', 'wb');
    
    foreach ( $fields as $line ) {
        $val = explode(",", $line);
        fputcsv($fp, $val);
    }
    
    fclose($fp);
    
    
    

    Output
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search