1

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 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hello, thanks for answering my question. Is it alright to ask if the contents are still headers for the columns?
Hello @charmjo in this contents are columns not a headers. thanks
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>";

  }

?>

6 Comments

Thank you for responding to my question. I was iterating the value since the values are stored in an array. These values are the selected results coming from a multiselect option. Was able to retrieve the values for it.
Alright, makes sense, was thinking it was a normal string value. All the same, did the above work out for you?
I tried implementing your solution but unfortunately, my code still doesn't work. Thanks for trying.
Then you'll have to try this instead. Check the answer for updates.
Let me know if you had any issues.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.