Reputation: 2780
Im busy on a website for a game. I'm currently at registration. Now the point is, that we want to let players put a code in their Ingame notes, so we can verify if a account really belongs to someone.
So I wrote a PHP class that can fetch the ingame notes. Now the one thing we still have to do, is set a javascript variable with these notes. So I can search through them for the code.
What I thought of, was to do a setInterval
, and then in that function set the variable every 250 milliseconds. It appears to be a little harder then that. My first guess is that it has some scoping issue that makes the variable unavailable from outside that setInterval
.
This is the JS I made:
var mission = "";
setInterval( function() {
var mission = "<?php echo Core\InGame::getNotes('TestPlayer');?>";
}, 250);
$("#mission").html(mission);
How is it possible that such a simple piece of code doesn't work?
I made a few things sure.
I included jQuery.
I've put the code in a JS file, which is included.
I have debugged the PHP code in this setting (without the setInverval).
Is there a way to make this possible?
Upvotes: 0
Views: 358
Reputation: 30567
You cannot execute PHP code on the client side. The way to do this would be to call the php code with ajax or load()
myscipt.php
<?php echo Core\InGame::getNotes('TestPlayer');?>
myscript.js
setInterval( function() {
$("#mission").load('myscript.php');
}, 250);
Note, 250 ms
is a very short interval and depending on network latency will not update every 250 ms
. Try a larger interval.
Upvotes: 4
Reputation: 3752
You can move the whole code inside tgrget function;
setInterval( function() {
$("#mission").html("<?php echo Core\InGame::getNotes('TestPlayer');?>");
}, 250);
Upvotes: 1
Reputation: 9637
try
var mission = "";
setInterval( function() {
mission = " some string ";
$("#mission").html(mission);
}, 250);
$("#mission").html(mission);
Upvotes: 1
Reputation: 26380
You posted your PHP code.
This PHP, once interpreted and sent to the browser as HTML, gives something like this :
var mission = "";
setInterval( function() {
var mission = "Hello";
}, 250);
$("#mission").html(mission);
What is inside the setInterval won't change. It has been interpreted server-side, before being sent to the browser, and will always be "Hello". The PHP code disappears completely. So your interval works, but it sets always the same value...
Upvotes: 1