简体   繁体   English

如何获取php的输出并将此输出传递给纯html页面?

[英]How to get the output of php and pass this output to pure html page?

This is a critical scenario.I have 2 pages such as 这是一个关键的场景。我有2页,如

output.php has dynamic table data with style which is get from excel.

<?php
ob_start();

error_reporting(E_ALL);
ini_set('display_errors', 1);

    set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
    include 'PHPExcel/IOFactory.php';


    /* EXCEL TABLE */

    $inputFileName2 = 'C:\inetpub\wwwroot\Monthly-EDM\Action_items.xlsx';


    try {
        $objPHPExcel1 = PHPExcel_IOFactory::load($inputFileName2);

    }
    catch (Exception $e) {
        die('Error loading file "' . pathinfo($inputFileName2, PATHINFO_BASENAME) . '": ' . $e->getMessage());
    }

    /* Open an Excel & count */
    $allDataInSheet = $objPHPExcel1->getActiveSheet()->toArray(null, true, true, true);

    $arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet


    $file = fopen($inputFileName2, "r");
    fclose($file);

      echo '<table width="800" border="0" cellspacing="0" cellpadding="0" align="center">';
     echo '<tr>';
                echo '</td>';   

             /* <th> code here */
                            echo '</td>';
                       echo ' </tr>';
                        echo ' <!-- Header blue row - end -->';


                    echo '<style>.shiva:nth-child(odd)      { background-color:#b9b8bb; }</style>';
                    echo '<style>.shiva:nth-child(even) { background-color:#e5e8e8; }</style>'; 

    for ($i = 2; $i <= $arrayCount; $i++) {

        $_SESSION["a"] = trim($allDataInSheet[$i]["A"]);

        $_SESSION["b"] = trim($allDataInSheet[$i]["B"]);

        $_SESSION["c"] = trim($allDataInSheet[$i]["C"]);

        $_SESSION["d"] = trim($allDataInSheet[$i]["D"]);

        $_SESSION["e"] = trim($allDataInSheet[$i]["E"]);

        $_SESSION["f"] = trim($allDataInSheet[$i]["F"]);

        $_SESSION["g"] = trim($allDataInSheet[$i]["G"]);

             echo ' <tr>';
              echo '<td>';

          echo '</td>';
             echo ' </tr>';

   }
             echo '<!-- table content - end --> ';
             echo '</table>';
             echo '</td>';
             echo ' </tr>';
             echo '</table>';

?>

    get.html (I want to get the output of output.php here)

Click to view the result of the output.php 单击以查看output.php的结果

How to get the output of output.php and pass this output to get.html page and show the output.Is this possible to access the php output from html page?? 如何获取output.php的输出并将此输出传递给get.html页面并显示输出。这是否可以从html页面访问php输出? Thanks in advance.Please help me to fix it. 提前谢谢。请帮我解决。

I'm not 100% sure, if this is what you mean. 如果这是你的意思,我不是百分百肯定的。 But if you make a file (foo.php) and put it in the same directory as where output.php is, and then add this to foo.php: 但是如果你创建一个文件(foo.php)并将它放在与output.php相同的目录中,然后将其添加到foo.php:

<?php
$myfile = fopen("output.php", "r") or die("Unable to open file!");
echo fread($myfile,filesize("output.php"));
fclose($myfile);
?>

Then that should print the content of output.php in your browser, as HTML (if you go to /foo.php in the browser). 然后,它应该在浏览器中打印output.php的内容,如HTML(如果你在浏览器中转到/foo.php )。

Is that it? 是吗?

Here is one solution (this solution assumes you are using Apache) 这是一个解决方案(此解决方案假设您使用的是Apache)

Create a .htaccess file at the root of your site. 在站点的根目录下创建.htaccess文件。 Put this in the .htaccess file: 把它放在.htaccess文件中:

AddType application/x-httpd-php .html .htm

This file will make Apache parse .html files as PHP. 该文件将使Apache解析.html文件为PHP。 Then in your get.html file, put this code: 然后在你的get.html文件中,输入以下代码:

<html>
    <head></head>
    <body><?php include('path/to/output.php'); ?></body>
</html>

If you view the get.html file in your browser, you will see the output of output.php 如果您在浏览器中查看get.html文件,您将看到output.php的输出

Here is an alternative solution using Javascript and AJAX: 这是使用Javascript和AJAX的替代解决方案:

Put the following code in your get.html file. 将以下代码放在get.html文件中。 This will make an ajax request to your output.php file and put the response in a <div id="output"></div> . 这将对output.php文件发出ajax请求,并将响应放在<div id="output"></div>

<html>
    <head></head>
    <body>
        <div id="output"></div>
        <script>
            var request = new XMLHttpRequest();
            request.open('GET', 'output.php', true);

            request.onload = function() {
                if (request.status >= 200 && request.status < 400) {
                    document.getElementById('output').innerHTML = request.responseText;
                } else {
                    // We reached our target server, but it returned an error
                }
            };

            request.onerror = function() {
                // There was a connection error of some sort
            };

            request.send();
        </script>
    </body>
</html>

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

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