我正在使用 PHP AJAX DATATABLE (在drupal中)。我正在创建一个函数来生成表记录的csv文件。 我创建了一个按钮来单击(在html中)以访问该链接(或者如果我通过url调用它。)
我做到了,我的功能(php Function)工作正常。
但是我如何创建一个csv取决于搜索值?我不想刷新我的页面,我认为ajax电话会帮助我。
但是如何在ajax调用中处理我的csv文件创建功能。
我的php功能
/* testing the excel output */
function export_to_excel_page() {
//testing
$filename = "file_name.csv";
$row_array = db_query('SELECT recipient_name , year , title , sector , region , commitment_million_inr FROM {#######} ');
drupal_add_http_header('Content-Type', 'text/csv; utf-8');
drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $filename);
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('recipient_name', 'year', 'title', 'sector','region','commitment_million_inr' ));
foreach($row_array as $key => $value) {
fputcsv($output, (array)$value);
}
}
答案 0 :(得分:0)
如果您正在使用on click事件,则可以使用JQuery $ .get函数来执行您想要的操作。
//change #button to whatever id or class name you are using in your html
$("#button").on("click",function(){//begin on click event
$.get("place path to the csv here",function(data){//begin function
//Use the data parameter passed in to the function to manipulate(parse)
//your data returned from your csv file here.
//if you want to see if there is a string inside of the data
//indexOf returns -1 if the item is not found
//Check to see if the searchterm is found or is not equal to -1
if(data.indexOf("searchterm") !== -1){//begin if then
//Do something if you find the searchterm string.
}//end if then
//if they are comma seperated values you can first
//turn the csv into an array
var csv = data.split(",");
//loop through the array do whatever you need to do
for(var i =0; i < csv.length; i++){//begin for loop
//if the array index equals the string something
if(csv[i] === "something"){//begin if then
//do something like turn the csv into an undordered list
}//end if then
}//end for loop
});//end function
});//end on click event