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 ?
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;