tags:

views:

109

answers:

6

Hello, I have 2 pages, one PHP and one Javascript. I'd like to pass the variable from this PHP script on one page:

$strFind="SELECT * FROM  cometchat_chatrooms_users WHERE userid=$curmemid";
$result=mysql_query($strFind) or die(mysql_error());
$row=mysql_fetch_array($result);
$room=$row['chatroomid'];

to this Javascript on another page:

var timestamp = 0;
var currentroom = $room;
var heartbeatTimer;
var minHeartbeat = 3000;
var maxHeartbeat = 12000;

How can I do this?

+6  A: 

Hello,

you can do this, when the Javascript is generated by PHP. Example given:

<?php
...
$strFind="SELECT * FROM  cometchat_chatrooms_users WHERE userid=$curmemid";
$result=mysql_query($strFind) or die(mysql_error());
$row=mysql_fetch_array($result);
$room=$row['chatroomid'];
...
?>
<script type="text/javascript">
var timestamp = 0;
var currentroom = <?php echo $room; ?>;
var heartbeatTimer;
var minHeartbeat = 3000;
var maxHeartbeat = 12000;
</script>
H3llGhost
That will just try to assign the value of the *JavaScript* variable `$room` (which is undefined) to `currentroom`.
David Dorward
No, that will assign the JavaScript variable `currentroom` with the value of the PHP variable `$room`.
H3llGhost
@David Dorward: No, it wouldn't, this code is absolutely correct, `$room` is between `<?php` and `?>`
nico
@nico — it has been edited since I made the comment. Previously it was just: `var currentroom = $room`.
David Dorward
This will break if $room is not a Number or stored in the database in a JS literal format (e.g. including the `"`s in a string or the `{` and `}` in an object).
David Dorward
@David: That is correct. I found the mistake and corrected it immediately.
H3llGhost
@David Dorward: Ah OK, sorry, I did not see it had been edited :)
nico
@nico — For some reason SO fails to keep a history if you make edits very soon after creating an answer. It is rather annoying.
David Dorward
Didn't OP say the JavaScript lives on a separate page from the PHP script? Or is the answer also suggesting to keep both on the same page?
BoltClock
can't keep both scripts on the same page, the chat room js file has too many dependencies to move around.
John Sims
Then you can use PHP Sessions, an input hidden or an AJAX Request to get the data.
H3llGhost
H3 i'm trying that now
John Sims
Well, you can get the contents of js with file_get_contents(), parse it with PHP (you could have some placeholder in the original js like ROOM_PLACEHOLDER and str_replace() it). No problem.
Richard Knop
A: 

The code will work as is. When the output (html and javascript) is generated by your php it will already have done the substitution. So your value will be inserted there as a literal. If it is a string, you'll still have to quote it though.

var currentroom = '$room';

will become

var currentroom = 'myroom';

in output.

Joeri Hendrickx
No it won't work that way, see H3llGhost answer for how to do it properly
nico
+1  A: 

for another page you can use $_SESSION

<script type="text/javascript">
var timestamp = 0;
var currentroom = <?php echo $_SESSION['room']; ?>;
var heartbeatTimer;
var minHeartbeat = 3000;
var maxHeartbeat = 12000;
</script>

For same file/script sequence- its very simple, no need of SESSION

   var currentroom = <?php echo $room; ?>;
Sadat
thanks for responding Sadat, the page this java script should go one is a .js page, <?php echo $_SESSION['room']; ?>; is not working.
John Sims
This is a fairly terrible example. Sessions are relatively complicated beasts and can lead to things such as race conditions very easily. It might be slightly better if it bothered to mention that you have to create a session and store data in it before you can pull anything out of it!
David Dorward
@Jhon, I know that well. According to your question, you need it in another page. Remember one thing, you cant execute php in JS file unless you permit it from core server configuration.
Sadat
+2  A: 

Another option is that of PHP outputting a hidden element with the variable in it and then JS reading it.

For instance

<?php
echo '<input type="hidden" id="myvar" value='.$val.' />';
?>

and then in JS

 var v = document.getElementById("myvar");
 // do something with v.value

Of course this is easily spoofable by the client, so take 2 cautions:

1) use this only if it is not a problem for any user to be able to see the value of the variable (e.g. by looking at the source)

2) if the JS does anything that can be possibly "dangerous" e.g. does an asynchronous call to a PHP page that does something in the DB with that value, be sure to have proper checks in the second PHP page (NOT in the JS) to ensure that the value had not been tampered with

nico
htmlspecialchars! htmlspecialchars! Whenever you put data that you don't know is safe into a document, use htmlspecialchars! We don't like XSS security holes!
David Dorward
As for the two warnings … they apply to any data you end up asking the client to send back to you. There is nothing special about embedding it in the HTML.
David Dorward
@David Dorward: sorry? Which part of the data are you assuming is not safe? As for the two warnings, my point is that the JS may be only doing something client-side, in which case data tampering problems are not an issue. If it's doing something server-side then you should do appropriate checks on the server, that's all.
nico
$val, we have no idea what it is.
David Dorward
@David Dorward: exactly, that's why I didn't sanitise it. If `$val` is a number you can just cast it to `int`, if it is pulled from the DB you can be sure is fine because you sanitised the input before putting it into the DB etc etc. It is really dependent from the situation, my whole point here was to use a hidden input.
nico
A: 

You cannot actually "pass" a variable anywhere. But only a scalar value. Frankly, you can pass only text data.
So, in case of javascript you have 2 options:

  1. To generate whole js code from PHP along with all it's variables. Much like to how you generate HTML.

  2. To request some variable from JS running in a browser, using AJAX technique.

Col. Shrapnel
A: 
Bhanu