Welcome Guest, Not a member yet? Register   Sign In
Spry Ajax PHP Codeigniter Smarty - Report in XLS formate header problem

Dear Sir,

I am using CI with Spry Adodb Ajax and smarty template. I have problem while creating a Excel file on submit when the submit is through Ajax if its a normal submit promptly starts to down load. Following is the code i am using.

/* make an xls file */
if (! function_exists ( 'makeXLS' ) ) {
function makeXLS( $data, $file, $title ) {
$i = 0;
$xls = "<table border='1' cellpadding='1' cellspacing='0' bgcolor=''>";
$keyrow = "";
$valrow = array();
/* dump ($data);
print("<br>") . $file;
print("<br>") . $title; */

foreach($data as $res) {
$keyrow = "<tr><td colspan='". sizeof($res) ."' bgcolor='#000000' align='center'><font color='#FFFFFF' size='2'><b>$title</b></font></td></tr><tr>";
$valrow[$i] = "<tr>";
foreach((array)$res as $key=>$val) {

/* manual field/display key name mangling here */
switch ($key) {

case "gc_id":
$key = "Global Company ID";
/* case "amount":
$val = "\$" . number_format($val,2);
case "amount_sum":
$val = "\$" . number_format($val,2);
break; */

if (preg_match("/amount/i","$key")) {
$val = "\$" . number_format($val,2);
if (preg_match("/date/i", "$key")) {

$val = substr($val, 0, -11);
$key = preg_replace('/_/', ' ',"$key");
$key = ucwords($key);

/* now add the key and value to the excel output table */
$keyrow .= "<td bgcolor='#cccccc'><b>$key</b></td>";
$valrow[$i] .= "<td>$val</td>";
$keyrow .= "</tr>";
$valrow[$i] .= "</tr>";
$xls .= $keyrow;
foreach ($valrow as $row) {
$xls .= $row;
$xls .= "</table>";

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename= ". $file ." ");
header("Content-Transfer-Encoding: binary ");

print $xls;

My submit button with Ajax it is printing my xls file on the html page rather then prompt for the download in xls format.

&lt;form action="/acctls/main/company_records" method="post"
name="get_transactions" id="get_transactions"
type="hidden" name="form_name" id="form_name" value="get_invoices" />

If I use the same form submit like below it works perfectly but I must use Ajax as its Ajax progression.

&lt;form action="/acctls/main/company_records" method="post"
name="get_transactions" id="get_transactions"&gt;&lt;input
type="hidden" name="form_name" id="form_name" value="get_invoices" />

Can you please guide if its a header setting problem as if the submit is not ajax it works perfectly.
I am keenly waiting for your response.

[eluser]Randy Casburn[/eluser]
I believe this problem is due to the async nature of your AJAX call, and not because of this function. It appears to be constructed properly and you've confirmed that with a synchronous call.

What I suspect is happening is the script continues to process the remainder of your JS script without waiting for you the results from the previous call. At some point in execution, your browser get's confused by the file showing up with no request pending (since the script continued) and it just defaults to "show it".

Try loading it in sync mode. loadContent(url, {async:false})



If I try to call it async:false it's let me download the xls properly but then my page not used to get update. Instead of placing result content in div tag it result comes in new web browser or tab. Can i down load and update content using ajax call for loading content.


[eluser]Randy Casburn[/eluser]
You cannot update content AND offer your download in the same AJAX call. They need to be separate calls...one to update content. One to download the file.


Thank you Randy. Finally it worked. I created two forms once with Ajax submit and one with normal submit. And pass all the parameters from one form to other using javascript. It looks little ugly but I could not found any other elegant way.


[eluser]Randy Casburn[/eluser]
You are very welcome.

Theme © iAndrew 2016 - Forum software by © MyBB