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 .

Dreamfire
  • 103
  • 2
  • 12
  • 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;` – Voitcus Apr 22 '15 at 11:46
  • possible duplicate of [How to pass variables and data from PHP to JavaScript?](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Alessandro Da Rugna Apr 22 '15 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 – nirmal Apr 22 '15 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) – Dreamfire Apr 22 '15 at 12:14

4 Answers4

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);
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • 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. – Dreamfire Apr 22 '15 at 11:52
  • 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? – Mihai Matei Apr 22 '15 at 12:40
  • 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();` – Dreamfire Apr 22 '15 at 12:44
  • 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 = ;` 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');` – Mihai Matei Apr 22 '15 at 12:50
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>";
 ?>
Adam Kaczmarek
  • 146
  • 2
  • 10
  • How can I substitute "//ajax request" `$jatek = new jatek( $_SESSION['id'] ); $jatek->epuletkesz();` place ? (ofc, its php parameter) – Dreamfire Apr 22 '15 at 12:05
  • @Dreamfire is that session id ? what epuletkesz() function will do? – nirmal Apr 22 '15 at 12:14
  • Create php script with `$jatek = new jatek( $_SESSION['id'] ); $jatek->epuletkesz();` and use it as ajax url – Adam Kaczmarek Apr 22 '15 at 12:19
  • 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) – Dreamfire Apr 22 '15 at 12:20
  • 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. – Adam Kaczmarek Apr 22 '15 at 14:59
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>
nirmal
  • 2,143
  • 1
  • 19
  • 29
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>
Nandan Bhat
  • 1,573
  • 2
  • 9
  • 21