I am having a function inside JQuery that when a condition is met calls a PHP function. Everything goes well until I can file_put_contents. It seems that must throw some kind of output that JQuery doesn't know how to interpret. Here is my code:
JQuery part, where $downloader is the class instance and finishedDownloading is a javascript variable:
if (finishedDownloading==<?php echo $downloader->_totalFiles ?>){
<?php $downloader->MergePDFs(); ?>
}
So far so good. And here is my php:
function MergePDFs()
{
$combinedFiles = "";
foreach ($this->_fileNamesArray as $filename) {
$combinedFiles .= file_get_contents($filename); //these are the urls of my files
}
echo "document.getElementById('test-if-finished').innerHTML = 'Test output: " . $this->_file . "'"; // this is for testing
//The above code works, the problem comes when I add the lines below
file_put_contents("all-files.pdf",
$combinedFiles);
If I comment the file_put_contents lines everything goes smoothly.
If I uncomment, when I run the code I get a crazy error "uncaught referenceError" stating my JQuery function is not defined.
Can somebody tell me what is going on?
Thank you
Edit: I think file_put_contents is returning some value that JQuery doesn't know what to do with.
Edit 2: This could not be done, even if i was able to get rid of the jquery error, the function gets executed on page load, not taking into consideration the if statement
In the jQuery part you should not call the PHP functions by including them in the javascript code. In you example the PHP code would be processed anyway, unless the if condition of the jQuery part meets or not.
Try something like that for jQuery:
if (finishedDownloading==<?php echo $downloader->_totalFiles ?>){
$.get('mergepdf.php');
}
And mergepdf.php like your code:
<?php
function MergePDFs()
{
$combinedFiles = "";
foreach ($this->_fileNamesArray as $filename) {
$combinedFiles .= file_get_contents($filename); //these are the urls of my files
}
echo "document.getElementById('test-if-finished').innerHTML = 'Test output: " . $this->_file . "'"; // this is for testing and works fine
file_put_contents("all-files.pdf",
$combinedFiles);
...
}
MergePDFs();
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