Click to See Complete Forum and Search --> : PHP and Javascript.. how to add this js into a php variable?


DeltaOne
09-13-2007, 03:05 PM
Hello everyone.

I have the following javascript variable:
var the_variable = (Math.random() * 40) + 20;

It would return a random number between 20 and 60.

Now, I would like to put this into a php call:
<?php
sleep(THE_VARIABLE);
?>


How can I do this? :-/


EDIT:
Something like this would work?

sleep(echo('<script>(Math.random() * 40) + 20;</script>'));


And, I reckon that the echo wouldn't give me integers but strings?


** I don't have the possibility right now to test it, but I need a correct answer... though I figure that it would work.. **

TheBearMay
09-13-2007, 03:21 PM
Since the PHP code is executed prior to the page, and prior to the JS you'll have to return control to PHP via a submit and pass it the value.

DeltaOne
09-13-2007, 03:30 PM
What do you mean?

Can you please try to give me an example?

Also, is this possible to accomplish only with php? I want to make that sleep() with a random number between 20 and 60, and if it can be done only with php then I don't need any extra js... though I fear not..

TheBearMay
09-13-2007, 03:46 PM
sleep (rand(20,60));

DeltaOne
09-13-2007, 03:59 PM
Ah yes, that was simple.

Thanks alot of reminding me!

DeltaOne
09-13-2007, 05:05 PM
Not to start a new Topic, but this is somewhat along the same lines as my other request here.


How can I make that sleep(X); work inside a javascript, that's inside php?
Like in this example here:
<?php
$the = sleep(10);
echo("

<script>
function a()
{ ".$the." }
</script>

");
?>


Somehow something like that won't work.. I wonder if the sleep command is triggered automatically when the php reaches that line, disregarding the javascript thingy that would make the sleep command trigger only when that function is run?

Sheldon
09-13-2007, 05:10 PM
you cant do that. if you want something to stop for a certian amount of time with user input you may be able to use

setTimeout('your_function()',2500);

DeltaOne
09-13-2007, 08:44 PM
I feared that would be the case.

I wanted to freeze the page for a while, setTimeOut() wouldn't do that unfortunately enough (or I don't know..)

TechEvangelist
09-13-2007, 08:55 PM
The problem is that you are trying to mix PHP, which executes on the server prior to sending the page, with JavaScript, which executes in the browser after it receives the page. They are two different events that do not communicate with each other.

TheBearMay
09-14-2007, 04:12 PM
Okay this is ugly, but.....

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
var Paused=0;

<?php
if (isset($_REQUEST["sleep"]) ) pauseApp($_REQUEST["sleep"]);
else echo "// first pass";
function pauseApp($length){
sleep($length);
echo "Paused =".$length.";";
}
?>

window.onload = function() {
if (Paused < 1){
alert ("Recalling with delay of 15 seconds");
document.forms["hiddenF"].submit();
} else
alert ("Returning from a "+Paused+" second pause");
}
</script>

</head>

<body>
Some random text
<form id="hiddenF" action="testPause.php" method="post"><input type="hidden" name="sleep" value="15"></form>
</body>
</html>

TheBearMay
09-14-2007, 04:17 PM
A little cleaner:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
var Paused=0;

<?php
if (isset($_REQUEST["sleep"]) ) pauseApp($_REQUEST["sleep"]);
else echo "// first pass";
function pauseApp($length){
sleep($length);
echo "Paused =".$length.";";
}
?>

if (Paused < 1){
alert ("Recalling with delay of 15 seconds");
document.location="testPause.php?sleep=15";
} else
alert ("Returning from a "+Paused+" second pause");

</script>

</head>

<body>
Some random text

</body>
</html>