I'm a total php noob, trying to teach myself thru these projects that I do, and one of them (a kind of online Survivor-type game) involves using a PHP script to read a file called 'players.txt' with the names of the remaining players and use the info in that file to fill in a dropdown menu on an HTML page. I'm doing this thru <script type="text/javascript" src="script.php"></script> (there's probably a term for this but I don't know it)
Anyway, I was having trouble getting the PHP to work so to make it simpler I developed the script in JavaScript and then tweaked the PHP so that it would echo the code that I knew worked, which was this:
//This is script_js.js
function init(){
var option1="1 playerone ";
document.getElementById("opt1").text=option1;
var option2="2 playertwo ";
document.getElementById("opt2").text=option2;
var option3="3 playerthree ";
document.getElementById("opt3").text=option3;
var option4="4 playerfour";
document.getElementById("opt4").text=option4;
}
The PHP:
<?php
//This is script.php
$file = fopen("players.txt", "r");
$x = 1;
echo 'function init(){';
while(!feof($file))
{
echo 'var option' . $x . '="' . fgets($file) . '";';
echo 'document.getElementById("opt' . $x . '").text=option' . $x . ';';
$x++;
}
fclose($file);
echo '}';
?>
The TXT file:
1 playerone
2 playertwo
3 playerthree
4 playerfour
The relevant HTML:
<html>
<head>
<script type="text/javascript" src="script.php"></script>
</head>
<body>
<form action="eliminate.php" method="post">
<p>
<select id="pnumber">
<option id="opt1" value="1"></option>
<option id="opt2" value="2"></option>
<option id="opt3" value="3"></option>
<option id="opt4" value="4"></option>
<option id="opt5" value="5"></option>
<option id="opt6" value="6"></option>
<option id="opt7" value="7"></option>
<option id="opt8" value="8"></option>
<option id="opt9" value="9"></option>
<option id="opt10" value="10"></option>
</select>
</p>
<p>
<input type="submit" name="submit" value="Find player name" />
</p>
</form>
</body>
</html>
Now, if I change the script called by the HTML to the JS version, it works perfectly. Can someone please help me figure out why the PHP does not?