Reputation: 433
No, this is not like the other questions:
I need to have a php variable in a client-side javascript file. I tried using ajax, but due to the application being Node.JS I cannot properly find the link to the php file on client side.
I'm making a draw my thing game and since its a small project I have the words in a phpfile. If I put them in javascript clients could use 'inspect element' to find all the answers, php doesn't allow them to.
Basically a word is given to one client that he has to draw, the other client guess that word. The 'server' should select a word that only the (drawing) client can see.
TL;DR: Get php variable in javascript within a node.js application. (Serverside or clientside doesn't matter)
Code so far
(Client) Word.js
$.ajax({
url: 'util.php',
type: 'POST',
dataType: 'json',
success: function(result){
console.log(result['word']);
},
error: function(){
console.log("Error retrieving word.");
}
});
Util.php
$words = array("", ""); shuffle($words);
$selectedWord = $words[0];
$rtn = array('word' => $selectedWord);
echo($rtn);
Upvotes: 0
Views: 49
Reputation: 37803
Where you have...
echo($rtn);
... you want ...
echo json_encode($rtn);
Otherwise what your script outputs is just the word "Array".
Upvotes: 2