PHP/Javascript - get php variable within javascript

Posted by McNabbToSkins on Stack Overflow See other posts from Stack Overflow or by McNabbToSkins
Published on 2010-05-07T10:57:48Z Indexed on 2010/05/07 11:08 UTC
Read the original article Hit count: 338

Filed under:
|

I have run into an interesting problem. I am currently developing php page and need to access a php variable within the javascript onload.

$(document).ready(function() {
     var temp = <?php $page_id ?>
}

is this valid? I know that this might seem weird and not be allowed but I am developing a page that has two popup windows. The windows are created using the same view template and there is no way to distinguish between each other. If I stored a hidden value on the page with information unique to the page like so

<input type="hidden" value="<?php $page_id ?> id="page_id" />

if there are two views open at the same time there is no way for me to get a unique page id like so

var temp = $("#page_id").val();

Because there are two views witht he same input id that is not unique. Long story short is it valid to reference a php variable in the javascript.

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript