dongyan5239 2015-08-05 06:12
浏览 70
已采纳

PHP - 序列化表单数据并启动Excel下载

Serialize Form Data

//start excel download
$("#btn_excel").click(function(){

        var str = $("#my-form").serialize();
        window.location.href = SITE_URL+"somecontroller/someAction?form_data="+str;
    });
//end excel download

The query string generated

start_date=1-Jul-15&end_date=1-Aug-15&id=11&description=My+Description&project_name_selected=Amazon+AWS

To fetch the output, I have used below but this does not work

parse_str($_GET['form_data'], $output);
print_r($output);

Output

Array
(
    [form_data] => start_date=1-Jul-15
    [end_date] => 1-Aug-15
    [id] => 11
    [description] => My Description
    [project_name_selected] => Amazon+AWS
)

As you can see the query string is broken and does not correctly fetches the data.

Tried below but this won't work as I need to initiate a download through Ajax; the response will simply vanish in the ether, as a normal Ajax response.

$.post(SITE_URL+"somecontroller/someAction",str, function( data ) {
});

I am directing the browser to the resource directly which will automatically detect that it's a file to be downloaded, and initiate the download.

How should I pass serialized form data and fetch output to initiate Excel download. Also could anyone please shed some light on what am I doing wrong here ?

  • 写回答

1条回答 默认 最新

  • douba4275 2015-08-05 06:25
    关注

    I think you just need to change this line:

    window.location.href = SITE_URL+"somecontroller/someAction?form_data="+str;
    

    to this:

    window.location.href = SITE_URL+"somecontroller/someAction?"+str;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置