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

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;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题