PHP从html文件中获取内容,然后将内容传递回原始html文件


PHP get something from a html file, then pass something back to the original html file

我是php新手,假设在目录中有一个index.html和一个cal.php。

index . html:

<head>
    <script>
        some java script here used to display graphs based on cal.php calculated results
    </script>
</head>
<body>
    <form action="cal.php" method="post">
        <div class="row x_title">
            <div class="col-md-6">
                <h3>Dashboard</h3>
            </div>
            <select class="form-control" id="myFileSelect">
                <?php
                echo '<option>Choose file</option>';
                foreach (glob('Report' . '/*.csv') as $file) {
                    list($af, $bf) = split("/", $file, 2);
                    list($filenam, $extnt) = split(".", $bf, 1);
                    echo '<option value="' . $bf . '">' . $filenam . '</option>';
                }
                ?>
            </select>
        </div>
        <input type="submit">
    </form>
</body>

cal.php

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>'n";
        $row++;
        for ($c = 0; $c < $num; $c++) {
            echo $data[$c] . "<br />'n";
        }
    }
    fclose($handle);
}
$output
/* pass the output back to original html */
/* let's say pass this php variable $output to the javascript on original html */
?>

我想张贴表单时,用户点击提交,传递一些值cal.php,做一些计算在cal.php文件,然后传递变量$输出到原始html的JavaScript变量。

我对php如何处理html页面有点空白。

有人能帮我吗?或者给我一些建议?

使用ajax就可以做到。来自index.html的ajax请求将调用cal.php, cal.php应该在ajax的成功函数中返回您想要的结果,您将获得响应。