1

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?

2
  • thanks for all the answers but none of this is working, I thought for sure the sessions one would work. Commented Jul 22, 2010 at 6:53
  • Or you can use an AJAX request or a hidden input. When you are using PHP sessions remember, that your JavaScript must be generated by PHP too. Commented Jul 22, 2010 at 7:21

7 Answers 7

8

You can do this, when the JavaScript is generated by PHP. Example given:

<?php
...
$curmemid = intval($externalValue);
$strFind = 'SELECT * FROM cometchat_chatrooms_users WHERE userid='.$curmemid;
$result = mysql_query($strFind) or die(mysql_error());

if (isset($result)) {
    $row = mysql_fetch_array($result);
    $room = $row['chatroomid'];
} else
{
    echo 'There is something wrong!';
    $room = -1;
}
...
?>
<script type="text/javascript">
var timestamp = 0;
var currentroom = <?php echo $room; ?>;
var heartbeatTimer;
var minHeartbeat = 3000;
var maxHeartbeat = 12000;
</script>
Sign up to request clarification or add additional context in comments.

12 Comments

That will just try to assign the value of the JavaScript variable $room (which is undefined) to currentroom.
@David Dorward: No, it wouldn't, this code is absolutely correct, $room is between <?php and ?>
@nico — it has been edited since I made the comment. Previously it was just: var currentroom = $room.
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).
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.
|
3
<?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 if(isset($room))echo $room; else echo ""; ?>;
var heartbeatTimer;
var minHeartbeat = 3000;
var maxHeartbeat = 12000;
</script>

Comments

2

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.

Comments

2

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

5 Comments

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!
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: 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.
$val, we have no idea what it is.
@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.
1

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; ?>;

3 Comments

thanks for responding Sadat, the page this java script should go one is a .js page, <?php echo $_SESSION['room']; ?>; is not working.
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!
@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.
0

try to use Session to store your room variable and use it with javascript by the echo instruction

1 Comment

A piece of code would be welcomed.
-2

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.

1 Comment

No it won't work that way, see H3llGhost answer for how to do it properly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.