我在 JQuery 中有一个函数,当满足条件时调用 PHP 函数。一切顺利,直到我可以 file_put_contents。似乎必须抛出某种 JQuery 不知道如何解释的输出。这是我的代码:
JQuery 部分,其中 $downloader 是类实例,finishedDownloading 是一个 javascript 变量:
if (finishedDownloading==<?php echo $downloader->_totalFiles ?>){
<?php $downloader->MergePDFs(); ?>
}
到目前为止一切顺利。这是我的 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);
如果我评论 file_put_contents 行,一切都会顺利进行。
如果我取消注释,当我运行代码时,我会收到一个疯狂的错误“uncaught referenceError”,指出我的 JQuery 函数未定义。
谁能告诉我这是怎么回事?
谢谢
编辑:我认为 file_put_contents 正在返回一些 JQuery 不知道如何处理的值。
编辑 2:这无法完成,即使我能够摆脱 jquery 错误,该函数也会在页面加载时执行,而不考虑 if 语句
最佳答案
在 jQuery 部分,您不应通过将 PHP 函数包含在 javascript 代码中来调用它们。在您的示例中,无论如何都会处理 PHP 代码,除非 jQuery 部分的 if 条件是否满足。
为 jQuery 尝试类似的东西:
if (finishedDownloading==<?php echo $downloader->_totalFiles ?>){
$.get('mergepdf.php');
}
并像您的代码一样合并pdf.php:
<?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();
关于javascript - 从 JQuery 内部调用 PHP 函数 file_put_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35427989/