php - write and download csv after posting using jquery -


i posting html table data json server side using jquery $.post writing whole table data csv file. not outputting csv downloadable user.

i want pop csv file (which happens when download file. know save or open box csv)

client side code

//selecteddata having data json   $.post('ajax/csv_download.php', {      selecteddata: json.stringify(selecteddata)  }, function(html){  }); 

server side code

   global $fh;     $fh = @fopen( 'php://output', 'w' );    $post_data = json_decode($_post['selecteddata']);    foreach ($post_data $arr)    {      $val1 = $arr->val1  ;      $val2 = $arr->val2 ;      $val3 = $arr->val3 ;      $val4 = $arr->val4 ;      $val5 = $arr->val5 ;       $out = array($val1,$val2,$val3,$val4,$val5);        fputcsv($fh, $out);       } 

sounds want write contents of csv file browser after setting 'content-type' 'text/plain' in php. depending how web browser configured, prompt user save/open file.

<?php    $content="name,age,height,weight,gender";    $file="persons.csv";    header("content-type: text/plain");    header("content-disposition: attachment; filename=$file");    header("content-transfer-encoding: binary");    header("pragma: no-cache");    header("expires: 0");    echo "$content"; ?> 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -