这段JS内嵌PHP的代码为什么不能执行

 <!DOCTYPE html>
<html>
    <body>
            <div>
                <button onclick = "one()">start!</button>
            </div>
            <script>
            <?php

                function one() {
                        if (!file_exists("error_handling.txt")){
                            die("The File Does Not Exist!");
                        }else{
                            $file = fopen("error_handling.txt","r");
                        };

                }

            ?>
            </script>

            <p id = "demo">???</p>
    </body>
</html>

是因为JS内不能embedPHP吗?但是好像也不是这个原因。麻烦大家帮忙看看了

没试过,估计是不支持。

建议放在外面,文件后缀名要为.php才可以.

你的服务器端的函数,就没有输出到客户端,one函数当然没有就报错了。楼主要搞清楚客户端和服务器端来

    <div>
        <button onclick="one()">start!</button>
    </div>
    <script>
        function one(){
        alert('function one')
        }

        <?php

            function one() {
                if (!file_exists("error_handling.txt")){
                    die("The File Does Not Exist!");
                }else{
                    $file = fopen("error_handling.txt","r");
                };

            }

        ?>
    </script>
        <div>
            <button onclick = "one()">start!</button>
        </div>
        <script type="text/javascript">
        function one(){
            console.log("<?php
            function two() {
                    if (!file_exists("error_handling.txt")){
                        echo "The File Does Not Exist!";
                    }else{
                        $file = fopen("error_handling.txt","r");
                        if($file){
                            echo "open success";
                        }else{
                            echo "open error";
                        }
                    };

            }
            two();
            ?>");
        }
  </script>