0

Two important parameters have a php file:

$szamol

This specifies the number of seconds to wait . ( Need to start from the current time ): When time is up , call these functions:

$jatek = new jatek( $_SESSION['id'] ); $jatek->epuletkesz();

I know I can not do it in PHP countdown , but I do not know to pass the two parameters in php to javascript function .

For example, javascript setTimeout () function might be good, but I can not give it to it that these two parameters php .

4
  • You may pass the variables eg. using document.location. The easiest way is to pass them as _GET variables (eg. document.location = 'http://myserver.com/myscript.php?variable='+variableNameInJavaScript; Commented Apr 22, 2015 at 11:46
  • possible duplicate of How to pass variables and data from PHP to JavaScript? Commented Apr 22, 2015 at 11:49
  • what is $jatek and epuletkesz(), so can help you better. do you want to pass it for timer or want to call this functions Commented Apr 22, 2015 at 12:05
  • $jatek is a class, and the epuletkesz(); is a function of the $jatek class. The main things: There are buildings ( Web game will be ) and if the player wants to develop the building will develop the tar button. Each building is added separately to the amount of time built up . (For example : 0:00:15 ) . The $szamol give the time in seconds , the current time I start the counter and when the time expires, the $jatek- > epuletkesz ()when times run out) Commented Apr 22, 2015 at 12:14

4 Answers 4

1

You can use an ajax request like:

var szamol = 6000; // 6 seconds
var timer = setTimeout(function(){
  $.post('/path/to/my/php/file.php', {}, function(data) {
     // do something with the response
  }, 'json');
}, szamol);
Sign up to request clarification or add additional context in comments.

4 Comments

My $szamol parameter value always changing, and the $jatek = new jatek( $_SESSION['id'] ); $jatek->epuletkesz(); too always changing, so i cant set a direct parameter.
then.. you can do 2 ajax requests.. first will get the $szamol and the second one will call that method.. I don't understand what do you mean with the second afirmation, that $jatek = new jatek( $_SESSION['id'] ); $jatek->epuletkesz(); always changing.. how it's changing?
Only mean the id will changing, but only i dont know how the do the ajax requests witch get he $szamol and the $jatek->epuletkesz();
There is no problem if the id is changing as it is stored in a $_SESSION variable.. If you don't want to do a first request to get the $szamol value then you can add it directly in the js script as var szamol = <?php echo $szamol ?>; after you calculated it before echoing. If you don't want to do like this, then you can make in get request to another file, other than the file which calls the method $jatek->epuletkesz();: $.get('/path/to/file/szamol.php', function(data) { szamol = data.szamol; }, 'json');
0

You can use ajax request.

Just print a setTimeout function with ajax call, for example:

 <?php 
  echo "<script>
   setTimeout(function(){
    //ajax request 
   }, $szamol);
  </script>";
 ?>

5 Comments

How can I substitute "//ajax request" $jatek = new jatek( $_SESSION['id'] ); $jatek->epuletkesz(); place ? (ofc, its php parameter)
@Dreamfire is that session id ? what epuletkesz() function will do?
Create php script with $jatek = new jatek( $_SESSION['id'] ); $jatek->epuletkesz(); and use it as ajax url
I give answer for this in up, but once more: $jatek is a class that instantiates with the session id . A function of epuletkesz() and $jatek. The main things: There are buildings ( Web game will be ) and if the player wants to develop the building will develop the tar button. Each building is added separately to the amount of time built up . (For example : 0:00:15 ) . The $szamol give the time in seconds , the current time I start the counter and when the time expires, the $jatek- > epuletkesz () when times run out)
With ajax request You can send any parameter You want to Your php script (with post or get). Create onClick event on button, that user uses for creating building. Event function will be setTimeout with ajax call. Then You can pass any parameter You want, ie: building type etc.
0

we can give you solution in jquery or javascript side, but reality is that you can not call your php funtion from front end side. so none of the above answer can help you.

if you have any front end side code then try

<?php
$szamol = 6000;

?>
<script>
var szamol = <?php $szamol ?>; // 6 seconds
var timer = setTimeout(function(){
  //your code here
}, <?php $szamol ?>);
</script>

Comments

0

Try this, I have named this file to start_timer.php, I hope you have the value $szamol before loading this page. This code waits till

 $szamol

seconds after the page loads and call the functions

 $jatek = new jatek( $_SESSION['id'] );
 $jatek->epuletkesz(); 

This is my code,

 <?php
 if(isSet($_GET['token']))
 {
 if($_GET['token'] == "1")
 {
 //  CALL THE FUNCTIONS NOW
 $jatek = new jatek( $_SESSION['id'] );
 $jatek->epuletkesz();


 }


 }
 ?>

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Title of the document</title>

 </head>

 <body>
 <input type="hidden" id="timer" value="<?php echo $szamol; ?>"/>




<script type="text/javascript">
 var timer_value
 $(document).ready(function()
 {
 timer_value = document.getElementById('timer').value;
 setTimeout(function()
 { 

 location.href="start_timer.php?token=1";

 }, timer_value);

 })

 </script>
 </body>

 </html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.