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 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口