Requesting data from php in javascript question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Van Dugall
    New Member
    • Mar 2009
    • 6

    Requesting data from php in javascript question

    Hello I have been writing a game in javascript which works fine but I wanted to step it up and request the data of the game from the server. I haven't really used php...all I know is from the Internet and I couldn't really find the answer I was looking for. I asked around and they said this is really easy to implement but I still can't get it to work.

    My problem is it doesn't work for some reason... I think I'm not calling the url of my php file correctly too. I have the code set in javascript already to take stings from my words and hints arrays but I want the data to be on the php file(server side). I hope that makes sense. Thanks for your replies BTW .

    Basically, all I want is my words and hints to be requested from the arrays in the php code instead having the arrays on the client side. I hope it as simple as it sounds.

    I have separate files for the javascript and the php:

    Code:
    <?php
    # create PHP array:
    $words = array(,"Encyclopedia","Marsupial","Philadelphia");
    $hints    = array(array("Related to the Animal Kingdom","Scary to most people","Crawled up the water spout","Title of a movie","A fear","8 legs"),
                     array("Has volumes","Endless knowledge","Sold door to door","Now online","Alphabetized","Literature"),
                     array("Mammal","Think pouches","Australia","A group in the animal kingdom","Down under","Things that jump"),
                     array("City in the US","Cheesesteak","Has a baseball team with the same first letter","New England","Pennsylvania","The City of Brotherly Love"));
    
    if (isset($_GET['param'])){
        //$words = $_GET['param'];
        $return_value = $words[$i];
            $return_value2 = $hints[$i];
    
        return $return_value;
            return $return_value2;
    }
    
    ?>

    This is the game code:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <h1>The Word Smith</h1>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
    
    //preload the six images first
    var face0=new Image()
    face0.src="d1.gif"
    var face1=new Image()
    face1.src="d2.gif"
    var face2=new Image()
    face2.src="d3.gif"
    var face3=new Image()
    face3.src="d4.gif"
    var face4=new Image()
    face4.src="d5.gif"
    var face5=new Image()
    face5.src="d6.gif"
    
    var url = "http://students.engr.scu.edu/~rnaderza/php/Lab8_Part1/WordSmith(Part2).php?param=";
    
    words = document.getElementById("words").value;
    var hints = document.getElementById("hints").value;
    var randomdice, i=0;
    var playerOneScore=0;
    var playerTwoScore=0;
    var player=1;
    var hasRolled=false;
    var currentWordIndex;
    
    function startAgain() {
     guessed = " ";
     document.game.hint.value = "";
     hasRolled = false;
     playerOneScore=0;
     playerTwoScore=0;
     currentWordIndex = Math.round((words.length - 1)*Math.random());
     toGuess = words[currentWordIndex].toUpperCase();
     document.game.currentPlayer.value = "Player 1, it is your turn!";
     displayToGuess();
     displayScores();
    }
    
    function displayToGuess() {
     pattern=""
     for(i=0;i<toGuess.length;++i) {
      if(guessed.indexOf(toGuess.charAt(i)) != -1)
       pattern += (toGuess.charAt(i)+" ")
      else pattern += "_ "
     }
     document.game.toGuess.value=pattern
    }
    
    function badGuess(s) {
     if(toGuess.indexOf(s) == -1) return true
     return false
    }
    function winner() {
     for(i=0;i<toGuess.length;++i) {
      if(guessed.indexOf(toGuess.charAt(i)) == -1) return false
     }
     return true
    }
    function guess(s){
     if(badGuess(s)){
     	if ( player == 1 )
     	{
     		player = 2;
     		hasRolled = false;
     		document.game.currentPlayer.value = "Player 2, it is your turn!";
     	}
     	else
     	{
     		player = 1;
     		hasRolled = false;
     		document.game.currentPlayer.value = "Player 1, it is your turn!";
     	}
     }
     else if(guessed.indexOf(s) == -1)
     {
     	if ( player == 1 )
     		playerOneScore++
     	else
     		playerTwoScore++
     	guessed = s + guessed
     }
    
     displayToGuess()
     displayScores()
    
     if(winner()) {
    	currentPlayerWins()
     }
    }
    function throwdice(){
    	if (hasRolled == false)
    	{
    		//create a random integer between 0 and 5
    		randomdice=(Math.round(Math.random()*5) + 1);
    		document.images["mydice"].src=eval("face"+randomdice+".src");
    		displayHint();
    	}
    	else
    		alert("Sorry, you have already rolled once this turn!!!");
    	hasRolled=true;
    }
    
    function displayHint(){
    	document.game.hint.value = hints[currentWordIndex][randomdice]
    }
    
    function displayScores(){
    	document.game.score1.value = playerOneScore;
    	document.game.score2.value = playerTwoScore;
    }
    
    function guessword()
    {
    var name=prompt("Enter your guess:","");
    if (name!=null && name!="")
      {
      	if ( toGuess == name )
      		currentPlayerWins()
      	else
      		currentPlayerLoses()
      }
    else
    {
    	currentPlayerLoses()
    }
    }
    
    function currentPlayerWins()
    {
    	gameWinner = "Player " +player +" has won the game!!!!";
    	alert(gameWinner)
    	startAgain()
    }
    
    function currentPlayerLoses()
    {
    	if ( player == 1 )
    		gameWinner = 2;
    	else
    		gameWinner = 1;
    	gameWinner = "Player " +gameWinner +" has won the game!!!!";
    	alert(gameWinner)
    	startAgain()
    }
    
    function giveUp()
    {
    	currentPlayerLoses()
    	startAgain()
    }
    
    function buyvowel()
    {
      if ( (player == 1 && playerOneScore > 0)
      		|| (player == 2 && playerTwoScore > 0) )
      	{
    	  var name=prompt("Enter a vowel:","");
    	  if (name!=null && name!="")
    	  {
    	  	guess(name.toUpperCase())
    	  	if ( player == 1 )
    	  		playerOneScore--;
    	  	else
    	  		playerTwoScore--;
    	  	displayScores();
    	  }
      }
      else
      {
      	alert("You do not have enough points to buy a vowel!!!");
      }
    }
    
    </script>
    <ul class="navbar">
      <li><a href="gamerules.html">RULES</a></li>
    </ul>
    <form NAME="game">
    
    Word to guess: <input type="text" name="toGuess" onfocus="stayAway()"><br><br>
    Hint: <input type="text" name="hint" size="90"><br><br>
    Player1_Score: <input type="text" size="10" id="score1" maxlength="20" /><br/>
    Player2_Score: <input type="text" size="10" id="score2" maxlength="20" /><br><br>
    <img src="d1.gif" name="mydice"><br><br>
    
    <input type="text" name="currentPlayer" size="30">
    <input type="button" value="Throw dice!" onClick="throwdice()">
    <input type="button" value="Buy a vowel" onClick="buyvowel()">
    <input type="button" value="Guess word" onClick="guessword()"><br><br>
    
    
    <INPUT TYPE="BUTTON" VALUE=" B " ONCLICK="guess('B')">
    <INPUT TYPE="BUTTON" VALUE=" C " ONCLICK="guess('C')">
    <INPUT TYPE="BUTTON" VALUE=" D " ONCLICK="guess('D')">
    <INPUT TYPE="BUTTON" VALUE=" F " ONCLICK="guess('F')">
    <INPUT TYPE="BUTTON" VALUE=" G " ONCLICK="guess('G')">
    <INPUT TYPE="BUTTON" VALUE=" H " ONCLICK="guess('H')">
    <INPUT TYPE="BUTTON" VALUE=" J " ONCLICK="guess('J')">
    <INPUT TYPE="BUTTON" VALUE=" K " ONCLICK="guess('K')">
    <INPUT TYPE="BUTTON" VALUE=" L " ONCLICK="guess('L')">
    <INPUT TYPE="BUTTON" VALUE=" M " ONCLICK="guess('M')">
    <INPUT TYPE="BUTTON" VALUE=" N " ONCLICK="guess('N')">
    <INPUT TYPE="BUTTON" VALUE=" P " ONCLICK="guess('P')">
    <INPUT TYPE="BUTTON" VALUE=" Q " ONCLICK="guess('Q')">
    <INPUT TYPE="BUTTON" VALUE=" R " ONCLICK="guess('R')">
    <INPUT TYPE="BUTTON" VALUE=" S " ONCLICK="guess('S')">
    <INPUT TYPE="BUTTON" VALUE=" T " ONCLICK="guess('T')">
    <INPUT TYPE="BUTTON" VALUE=" V " ONCLICK="guess('V')">
    <INPUT TYPE="BUTTON" VALUE=" W " ONCLICK="guess('W')">
    <INPUT TYPE="BUTTON" VALUE=" X " ONCLICK="guess('X')">
    <INPUT TYPE="BUTTON" VALUE=" Y " ONCLICK="guess('Y')">
    <INPUT TYPE="BUTTON" VALUE=" Z " ONCLICK="guess('Z')"><BR><BR>
    
    <INPUT TYPE="BUTTON" NAME="restart" VALUE="---- Start Again ----" ONCLICK="startAgain()">
     <INPUT TYPE="BUTTON" NAME="giveup" VALUE="---- Give Up? ----" ONCLICK="giveUp()">
    <script language="JavaScript"><!--
    startAgain()
    // --></script>
    
    </form>
    </body>
    </html>
    Regards,
    Van Dugall
  • brixton
    New Member
    • Nov 2006
    • 35

    #2
    While it's still not that pretty to store the data in raw PHP format, I would in your case go for a simple Ajax call from the Javascript. So if you haven't read up on Ajax, do so, it's really neat.

    Comment

    Working...