I have a simple contact form for download excel file . Main issue happen , When ajax load .I want to download excel file then redirect user to a next page.. Below is my code with dummy data..
Ajax code..
$.ajax({ type: "POST", url: "/site/ajaxexcel.php", data: {'value':'send'}, cache: false, success: function(html){ location.href = '<?php echo base_url()."/site/welcome.php" ?>'; } });
And my ajaxexcel.php
code is:
<?php $content= '<html xmlns:x="urn:schemas-microsoft-com:office:excel"> <head> <!--[if gte mso 9]> <xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name>Sheet 1</x:Name> <x:WorksheetOptions> <x:Print> <x:ValidPrinterInfo/> </x:Print> </x:WorksheetOptions> </x:ExcelWorksheet> </x:ExcelWorksheets> </x:ExcelWorkbook> </xml> <![endif]--> </head> <body><table class="table table-condensed table-striped table-hover table-bordered pull-left" id="myTable"><thead><tr><th>Rakesh</th><th>kumar</th></tr></thead><tbody><tr><th>Rakesh</th><th>Rakesh</th></tr><tr><th>Rakesh</th><th>Rakesh</th></tr><tr><th>Rakesh</th><th>Rakesh</th></tr><tr><th>Rakesh</th><th>Rakesh</th></tr></tbody></table></body></html>'; header('Content-type: application/excel'); header('Content-type: image/jpeg,image/gif,image/png'); header("Content-Disposition: attachment; filename=download.xls"); header("Pragma: "); header("Cache-Control: "); echo $content; ?>
I want to just download excel file and then redirect user to a specific location.
Also you can help me with your codeigniter code if you have done it properly..
ajax({ type: 'POST', url: 'b. php', data: 'result='+$name, success: function() { window. location. href = "profile.
We cannot download the file through Ajax, must use XMLHttpRequest.
$. ajax({ type: 'POST', url: 'AJAX URL', data: "YOUR DATA" // don't forget to pass your csrf_token when using post success: function(data){ $("what_ever_you_want_to_replace"). html(data. view); }, error: function(xhr, type, exception) { // if ajax fails display error alert alert("ajax error response type "+type); } });
Add a middleware to process response, if it is a redirect for an ajax request, change the response to a normal response with the redirect url. Then in ajaxComplete, if the response contains redirect, it must be a redirect, so change the browser's location.
Try below method, hope it will work
ajaxexcel.php
in new window via window.open
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With