Guy Fieri Guy Fieri - 3 months ago 10
PHP Question

Adding PHP to script causes rest of JS functions to stop working

<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>


I'm trying to move the array to a text file so it keeps its values after closing but as soon as I add the PHP code underneath all my other functions stop working. The PHP code itself works, creating the seats file and populating it, but everything afterwards doesn't.

for school

Answer

Can you show us the content of print_r($seatsArray);

I'm sure this, make an JS error, and everything else in JS stop working.