简体   繁体   English

从JQuery内部调用PHP函数file_put_contents

[英]Calling PHP function file_put_contents From Inside JQuery

I am having a function inside JQuery that when a condition is met calls a PHP function. 我在JQuery中有一个函数,当满足条件时调用PHP函数。 Everything goes well until I can file_put_contents. 一切顺利,直到我可以file_put_contents。 It seems that must throw some kind of output that JQuery doesn't know how to interpret. 似乎必须抛出JQuery不知道如何解释的某种输出。 Here is my code: 这是我的代码:

JQuery part, where $downloader is the class instance and finishedDownloading is a javascript variable: JQuery部分,其中$ downloader是类实例,finishedDownloading是一个javascript变量:

if (finishedDownloading==<?php echo $downloader->_totalFiles ?>){

    <?php $downloader->MergePDFs(); ?>

}

So far so good. 到现在为止还挺好。 And here is my 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

        //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. 如果我评论file_put_contents行,一切顺利。

If I uncomment, when I run the code I get a crazy error "uncaught referenceError" stating my JQuery function is not defined. 如果我取消注释,当我运行代码时,我得到一个疯狂的错误“未捕获的referenceError”,说明我的JQuery函数没有定义。

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. 编辑:我认为file_put_contents正在返回一些JQuery不知道该怎么做的值。

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

In the jQuery part you should not call the PHP functions by including them in the javascript code. 在jQuery部分中,您不应该通过将它们包含在javascript代码中来调用PHP函数。 In you example the PHP code would be processed anyway, unless the if condition of the jQuery part meets or not. 在您的示例中,无论如何都将处理PHP代码,除非jQuery部分的if条件符合或不符合。

Try something like that for jQuery: 为jQuery尝试类似的东西:

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

And mergepdf.php like your code: 和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