I need to send data to php file and download it.
My script is working correctly when I call it directly, but when I send data with AJAX it doesn't download at all.
My question would be: How to send data to php file and download file automatically but of course stay on the same page?
Part of the code that is working when called directly...
PHP file
header('Content-Description: File Transfer');
header("Content-type: application/ms-word");
header("Content-Disposition: attachment;Filename=ponuda.doc");
$productsArr = json_decode($_POST['object']);
$html = "<tr>";
foreach($productsArr as $product)
{
//something
}
....
echo $html;
AJAX call:
$.ajax({
type: "POST",
url: "test_download.php",
data: { object:productsJSON },
cache: false
});
I do the following:
Include the exchanger.js javascript file in your head section
Initialize the exchanger object on page load: theBuffer = new exchanger('dwnld');
Create a javascript function that you will call whenever you want to initiate a file download
:
function downloadFile(){
// you can add parameters to the function as needed to pass in dynamic data sent to the back end download handler
data = "http://your_backend_file_download_handler.php?param1=val1¶m2=val2&etc=whatever"; // send whatever data you need to the php program via query string parameters
theBuffer.sendData(data); // initiate the file download
}
Note: The php back end file download program that handles the requests can do whatever it needs to do with the parameters you send it in order to put together/retrieve the correct data/file for download. After much tinkering this combination is what consistently works for me
Include this little bit of html in your body section. I usually put it just before the closing body tag:
<iframe name="dwnld" id="dwnld" style="width:0;height:0;border:0">
</iframe>
Note: the id value assigned to the iframe is the same value given in step 2 when initializing.
The result is that the user never leaves the current page to download any number of files because the actual download is handled in a separate page (aka the iframe). I have used it without issue in all of my projects for years now.
I think You can't send header to browser by ajax, but You can use this, great thing.
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
$.fileDownload('file.mp3')
.done(function () { alert('File download a success!'); })
.fail(function () { alert('File download failed!'); });
Related
I think I have this narrowed down to being a problem with trying to download a file through the AJAX request. I need to push the array to the php script, but the zip download that the script pushes back has to be redirected to a new window. Not sure how to fit that into my jquery.
I'm not sure if it counts as a duplicate to other questions involving downloading from php, because the ones I looked at didn't pass data through the ajax call.
I am passing an array from jquery to php through AJAX and getting a streaming zip download of files from my database. The script works when I navigate to it directly and downloads the zip file of files as expected. It does not however work when I try to trigger it through my jquery. I believe this has to do with the download needing to be redirected to a new window?
Jquery:
if (confirm("Download data for the following samples:" + download_list.toString())) { alert("Download Beginning - Will take several minutes per sample selected - DON NOT NAVIGATE AWAY FROM PAGE.");
$.ajax({
type: "POST",
url: myURL + "zip_download.php",
data: {download_listArray:download_list},
success: function(){
alert("OK");
}
});
PHP:
<?php
$sample_name_list = $_POST['download_listArray'];
foreach ($sample_name_list as $i => $sample_name){
//do stuff
}
//stream zip
?>
I use jquery to set a get query to a php script which then queries the database and writes to the screen, but I can't get it to trigger the download, even with headers.
The steps are as follows:
create a link that the user clicks to download the data
javascript sends the query parameters to php
php queries the database and writes the file
client downloads the file
But I can't get step 4 to happen.
Step 1: (this is a table object that also contains the parameters:
d3.select("#some-div").append('a")
.attr("href", "javascript: void(0)")
.on("click", function() { this.saveAsCSV() };
Step 2: Javascript file to make query:
var saveAsCSV = function(params) {
var tmp_params = $.extend({}, params);
tmp_params['State'] = "NM";
$.get('php/get_data.php', tmp_params);
}
php to return query:
...
header("Content-type: application/text-csv");
header("Content-Disposition: attachment; filename=query_result.csv");
while($row = $result->fetchArray() {
print "$row";
}
...
It works fine in that it correctly queries and will print the data in the javascript function (so it will print it to console.log if I add that into the get return function), but I can't figure out what I should do differently to make it just download it directly.
One thing I've tried is to do the following on the params object:
var param_string = encodeURIComponent(JSON.stringify(params));
location.href = 'http://www.mysite.com"+param_string;
But that both takes the user away from the page and fails to download the data.
EDIT: I should clarify that the php file does output the query well in csv format. The problem seems to be that using the $.get() function does not trigger a download regardless of the php headers. Maybe I need to just provide a simple link with the parameters in the URL address, but I'm not sure how to get a javascript object into a URL format so that the php script can interpret it.
You could open a popup/new window/tab/whatever with your URL php/get_data.php?State=NM (perhaps additional parameters). It should download the output.
But your output might be wrong because you just print the variable $row which is an array. If you try to print an array that way it will just show Array.
You will need to properly output your rows. Unfortunately I don't know the CSV structure well enough to help you with that problem.
You can make an AJAX call for this using something like jQuery and it will pop up the download box while keeping the user on the page. Do something like this:
$.ajax({data: {download: 'query_result.csv'}, type: 'GET', url: 'download.php', cache: false });
I've tried this a few times for a previous employer and it always worked great. Although I did it mostly with .zip and .docx files.
I figured it out!
Basically, my encoding was wrong. I don't want to encode with
encodeURIComponent(JSON.stringify(params));
The result isn't readable by the php script. However, it works to just use $.param().
To summarize, the download is triggered by creating the URL link and then using location.href to link to it. Hence everything else is the same, but instead of the $.get() in step 2, I do:
var url_params = $.param(tmp_params);
location.href = url_params;
Which generates the download. Thanks!
I'm trying to figure out how this is posssible.
I want the user to be able to click on a link, and it will download a certain file. The file will be determined in an Ajax call (right now I just have a hard coded value).
Here's my situation though: When Ajax is called, the link given for the request is part of my framework which has a front controller and the url gets redirected. Thus, I can't just send the link to a file named like download.php and have just header code in there. It goes through the whole framework process, and eventually gets to the method that handles the Ajax call.
Here's my code:
$('.jobApplicationDownload').click(function() {
var that = $(this);
$
.ajax({
type : "POST",
url : "myfw/businessHome/applications/downloadJobApplicationItem",
data : {
"ajaxFileType" : that.attr("data-ftype"), "ajaxApplicationId" : that.attr("data-did")
},
success : function(html) {
alert(html);
},
error : function(XMLHttpRequest,
textStatus, errorThrown) {
alert("ERROR");
}
});
});
And here's the PHP. Remember this is after going through a Front Controller and a bunch of other methods. This is by no means the first stop after the Ajax call.
function downloadJobApplicationItem() {
//download.php
//content type
$fileName = "/myfw/common/jobs/resumes/eZACKe_1359081853_Week Three Sprints & Hurdles Workout 24th - 28th of Sept (1).pdf";
header('Content-disposition: attachment; filename='.$fileName);
header('Content-type: application/pdf');
//read from server and write to buffer
readfile('/myfw/common/jobs/resumes/eZACKe_1359081853_Week Three Sprints & Hurdles Workout 24th - 28th of Sept (1).pdf');
echo $_POST['ajaxFileType']. " ". $_POST['ajaxApplicationId'];
}
EDIT: Oh, and by the way, the result of this is the stuff I'm echoing being alerted, and no file download starting.
I'm confused when reading your question because I think you are getting mixed up between multiple questions.
Does the AJAX request reach the right handler?: the URL routing mechanism used in your framework and almost every other only matters here, and it doesn't affect whether you can reach your physical file. It's an entirely different matter. Regardless, I'd say your AJAX call is correct because the echo gets alerted
How to send the path to a file via AJAX call that gets redirected?: One trick you can consider using, which is similar to what you did, is to have a central download function as AJAX handler AND construct the path to the file using parameters sent with AJAX.
Why doesn't readfile output the desired file to the buffer: it's most likely that your path is incorrect or incompatible with server setting. It'd help if you could post your server configuration here.
And posting the name of your framework doesn't hurt either :)
I've solved this problem by buffering all the output and allowing the controllers to change or unset the redirect location. After all the controllers run, check the redirect location and write the redirect header (if necessary) at that time. Then output your buffer.
Something like this:
$appSettings = new AppSettings();
$appSettings->setRedirect(...);
$controller=new Controller($appSettings);
ob_start();
$controller->run(); //Your controller can call $appSettings->setRedirect(false);
$redirect=$appSettings->getRedirect();
if($redirect===false)
{
ob_end_flush(); //Send the buffer
}
else
{
ob_end_clean(); //Discard the buffer
header('Location: '.$redirect);
}
I am trying to get the image links from 9gag (what also works) and when I click on a button the image changes to the next one. The basic problem is that it works only once. I can then switch between the 1st and the 2nd image, though. This should be pretty simple, but I ´ve got no clue where the error is, so thanks in advance to anyone bothering to look at this.
<?php
$index = 0
$html = file_get_contents("http://www.9gag.com");
preg_match_all( '|http://d24w6bsrhbeh9d\.cloudfront\.net/photo/.+?\.jpg|', $html, $gags);
?>
<script>
function nextImg(){
<?php $index++;?>
pic.src='<?php echo $gags[0][$index];?>';
}
function prevImg(){
<?php $index--;?>
pic.src='<?php echo $gags[0][$index];?>';
}
</script>
You can't increment your PHP variables after the page has loaded. You are trying to increment them client-side with JavaScript. You are going to need to call that PHP using AJAX if you want to do this without refreshing the page, and even then you'll want to increment a javascript variable to keep track of where you are.
EDIT: I went a little nuts creating an ajax routine using PHP and JavaScript, specifically the jQuery library, which you will need to link to for this to work. You may also need to modify parts of the script to work with what you're trying to accomplish, but this certainly is a guide for running your ajax app as you're hoping to.
Start by making a PHP file with this script:
<?php
// Set content header to json
header('Content-Type: application/json');
// Get the index from the AJAX
$index = $_GET['index'];
// Grab file contents & parse
$html = file_get_contents("http://www.9gag.com");
preg_match_all( '|http://d24w6bsrhbeh9d\.cloudfront\.net/photo/.+?\.jpg|', $html, $gags);
// Send filename back to AJAX script as JSON
echo json_encode(array($gags[0][$index]));
?>
Then, in your HTML, include this jQuery to complete AJAX calls to your PHP script, and update the DOM with the data from the PHP script.
<script>
$(function() {
'use strict';
// Initiate index variable
var index = 0;
// Load initial image
loadImage(index);
// Add click event to a button with class of next-btn
$('.next-btn').click(function(e) {
e.preventDefault();
// Increment index to get next image
index++;
// Run AJAX function to retrieve image
loadImage(index);
});
// Add click event to a button with class prev-btn
$('.prev-btn').click(function(e) {
e.preventDefault();
// Decrement the index if it isn't 0
if (index > 0) {
index--;
}
// Run AJAX function to retrieve image
loadImage(index);
});
});
function loadImage(index) {
'use strict';
$.ajax({
type: 'GET',
url: 'your-php-script.php', // Filepath to your PHP script
data: 'index='+index, // Index is passed through GET request
dataType: 'json', // Return JSON
success: function (data) { // If the php script succeeds
// Change img with class of pic's src
// to the filename retrieved from php
$('.pic').attr('src', data[0]);
}
});
}
</script>
Configuring this for your needs will require some serious PHP and jQuery/JavaScript knowledge, as some debugging will likely be needed. Good luck!
EDIT 2:
I uploaded the working (tested, it works) source files to my website if you want to download. Please accept answer and let me know you grabbed the files...
http://www.wedgewebdesign.com/files/ajax-image-loader.zip
#Eric basically has it right but didn't really go into detail if you aren't familiar with the model...
PHP is a server side language in that it does all its processing on the web host server and once it is complete sends a static result back to the user. This means, whatever you see after the page is loaded within PHP is there to stay, unless you do one of two things:
1) Send a new request -- You provide different parameters, the page re-executes its logic and returns a new result to the user
2) Execute some form of clientside Javascript. Javascript is different from PHP in that it executes on the client (not the server) so you don't necessarily have to send responses back to the server unless you need more information. Javascript and PHP can be combined to create AJAX calls which allow the client to make asynchronous calls to the webserver for more data without reloading the entire page. The Javascript handles re-drawing the new information or updating the page which can appear seamless to the user.
What you therefore need is one of those two options. Either you provide 'next'/'previous' links to the user and the page is loaded differently each time or you create an AJAX call that fetches the url of the next image and then loads it.
Try assigning a variable to $gags[0][$index]. Something like
$imgsrc = $gags[0][$index];
and then
pic.src='<?php echo $imgsrc; ?>';
I have a PHP function
function ExportExcel()
{
// code
}
and a link on the page Download in Excel
<a>Download in Excel</a>
So what I want is when users clicks on that link, PHP function would be called and data will be downloaded in excel.
I may need to Ajax for that. How do I go about doing that ?
You could possibly just use a GET statement, so it would look something like this...
HTML
Download in Excel
PHP
function ExportExcel()
{
// code
}
if($_GET['init'])
{
ExportExcel();
}
here is the function i implemeted recently:
$('#toexcel').live("click",function() {
$.ajax({
url: "toExcel.php",
data: "sql="+encodeURIComponent(sql),
beforeSend: function(){
$("#wait").show();
},
complete: function(){
$("#wait").hide();
},
success: function(response){
window.location.href = response.url;
}
});
});
where sql variable actually stores sql query to the server,
and then toExcel.php if getting passed sql, submitting it to the server and outputs the result using PHPExcel() object.
EDIT
i think i understood what you trying to achieve. your ExporExcel() function already outputs the results you need, right? is so, then you can do it as follow:
$('#toexcel').click(function() {
$.ajax({
url: "toExcel.php", // should contain and _call_ you ExportExcel() function
beforeSend: function(){
$("#wait").show(); // this is loading img to show
},
complete: function(){
$("#wait").hide(); ;// this is loading img to hide once complete
},
success: function(response){
window.location.href = response.url;
}
});
});
first let me make sure you know php is only parsed when the page is first being distributed. If you click a link on the page, it has no idea the php function on the same page exists because the function only existed server-side while the code was being parsed. That being said, you can easily make a separate page called download.php and call your function on that page. Then your link can just link to that page.
If you want your custom download page to return to the user as an excel file, you can use custom php headers to convince the browser that it is downloading an excel file. (you'd have to specify the MIME type for excel files)
edit:
this would cause a download to start of an excel file created by your function call and activated by your link click. You don't need any JS or JQuery for this.
edit2:
here's example code for the download file to get you started
<?php
header("Content-type: application/excel");
print($data); /* print out the contents of the excel file here */
exit();
?>
If you do it like this, your php page will not redirect from your original page, but will bring up a download box from the browser instead. If your using csv files instead of xls files, you'll need to change the mime type.
you can handle the request in your js scrpit file
$("a").click(function(){
jQuery.ajax({
url: "path/to/controller",
type: "POST",
dataType: 'json',
data: {'mentod':'ExportExcel'},
success: successCallback,
error:failureCallback
});
});
Just provide link of that excel file in href of anchor , browser will download automatically
If your file form DB then providelink of excel.php , and in excel.php do processing of getting excel file and creation of it .
read this artical..do like that