javascript - 将 PHP 添加到脚本会导致其余 JS 函数停止工作

标签 javascript php

<script>
var array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

<?php
    $seatsArray = array();
    $myFile = fopen("seats.txt", "w") or die("Unable to Open File!");
    if(filesize("seats.txt") == 0) {
        for($x = 0; $x < 10; $x++) {
            fwrite($myFile, "0\n");
        }
    }   
    $seatsArray = file("seats.txt", FILE_IGNORE_NEW_LINES);
    fclose($myFile);
    print_r($seatsArray);
?>

function checkNumber() {
    var number = document.getElementById("userInput").value;
    if(number == 1) {
        firstClass();
    }
    else if(number == 2) {
        economyClass();
    }
    else {
        document.getElementById("message").innerHTML = "Please enter either 1 or 2.";
    }
}

function firstClass() {
    for(var x = 0; x <= 4; x++) {
        if(array[x] == 0) {
            array[x] = 1;
            document.getElementById("message").innerHTML = "You have been assigned seat #" + (x+1) + " in first class.";
            document.getElementById("f" + (x+1)).style.color = "red";
            document.getElementById("userInput").value = "";
            break;
        }
        else if(array[4] == 1) {
            document.getElementById("frow").style.color = "red";
            if(array[9] == 1) {
                document.getElementById("message").innerHTML = "Sorry, the flight is fully booked. The next flight is in three hours.";
            }
            else {
                var confirmation = window.confirm("First class is fully booked, would you like a seat in economy?");
                if(confirmation) {
                    economyClass();
                    break;
                }
                else {
                    document.getElementById("message").innerHTML = "The next flight is in three hours.";
                    break;
                }
            }
        }
    }
}

function economyClass() {
    //ETC ETC

</script>

我正在尝试将数组移动到文本文件,以便它在关闭后保留其值,但是一旦我在下面添加 PHP 代码,所有其他函数就会停止工作。 PHP 代码本身可以工作,创建席位文件并填充它,但之后的所有内容都不起作用。

学校用

最佳答案

你能告诉我们print_r($seatsArray);的内容吗

我确定这一点,如果发生 JS 错误,JS 中的其他所有内容都会停止工作。

关于javascript - 将 PHP 添加到脚本会导致其余 JS 函数停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38892658/

相关文章:

php - 使用 Symfony 将用户数据安全地存储在数据库中

php - 无法上传媒体,在我的 WordPress 网站上也看不到 GridView ?

php - 如何设置 Facebook 返回 URL

php - 对类使用 @covers

javascript - 如何使用 css 或 jq 在 laravel 中创建图像 slider

JavaScript 链接点击计数器

javascript - 使用 'useState' 将图像文件添加到 react 状态

javascript - 想要检查 NodeJS 中 console.log 的函数体

javascript - PHP 无法从 Ajax Jquery 捕获数据

PHP:如何防止用户在登录后单击后退按钮时进入登录页面?