繁体   English   中英

从JQuery内部调用PHP函数file_put_contents

[英]Calling PHP function file_put_contents From Inside JQuery

我在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行,一切顺利。

如果我取消注释,当我运行代码时,我得到一个疯狂的错误“未捕获的referenceError”,说明我的JQuery函数没有定义。

有人能告诉我发生了什么事吗?

谢谢

编辑:我认为file_put_contents正在返回一些JQuery不知道该怎么做的值。

编辑2:这无法完成,即使我能够摆脱jquery错误,该函数在页面加载时执行,而不考虑if语句

在jQuery部分中,您不应该通过将它们包含在javascript代码中来调用PHP函数。 在您的示例中,无论如何都将处理PHP代码,除非jQuery部分的if条件符合或不符合。

为jQuery尝试类似的东西:

if (finishedDownloading==<?php echo $downloader->_totalFiles ?>){
    $.get('mergepdf.php');
}

和mergepdf.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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM