<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代码本身可以工作,创建席位文件并填充它,但之后的所有内容都没有。
上学
答案 0 :(得分:0)
你应该使用angular $ http服务来使用你的php,如果你真的需要它:
$http({
method: "POST",
url: "YOUR.php",
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
答案 1 :(得分:0)
您能否向我们展示print_r($seatsArray);
我确定这个,发生JS错误,JS中的其他所有内容都停止工作。
答案 2 :(得分:0)
你不能只是print_r(file())
- PHP风格的数组导致JS错误。
如果你想显示这个文件的内容,你应该按行enter link description here逐行显示
foreach ($lines as $line_num => $line) {
echo htmlspecialchars($line);
}