admin管理员组

文章数量:1416331

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 with the same input id that is not unique. Long story short, is it valid to reference a php variable in the javascript?

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 with the same input id that is not unique. Long story short, is it valid to reference a php variable in the javascript?

Share Improve this question edited Jul 19, 2011 at 8:21 Lightness Races in Orbit 386k77 gold badges666 silver badges1.1k bronze badges asked May 7, 2010 at 10:57 McNabbToSkinsMcNabbToSkins 2571 gold badge7 silver badges17 bronze badges 1
  • See also: stackoverflow./questions/1808108/… – Marc Gravell Commented May 7, 2010 at 12:06
Add a ment  | 

6 Answers 6

Reset to default 1

Long story short is it valid to reference a php variable in the javascript.

Short answer, yes you can...PHP is server-side language, you can use it where you want.

Note: I assume that you are doing this in a file with php extension.

Long story short is it valid to reference a php variable in the JavaScript?

You are not referencing a PHP variable in JavaScript. You are simply generating the JavaScript code dynamically through PHP, where the value of the PHP variable $page_id gets hardcoded into the JavaScript code.

If you generate your JavaScript code through PHP, and you use var temp = <?php echo $page_id ?> it will work, but I wouldn't consider it best practice for bigger projects. I prefer my JavaScript code to remain static.

Your first piece of code is valid as long as you are generating the javascript. The same wont work if you put your js code in a separate .js file. Generating dynamic js is not a good practice for several reasons, like js browser caching and reuse for example.

If you want to pletely separate the js code of php, you can create a client-server munication where js will ask for a specific value from a php script through ajax and later play with it in js environment.

The only thing you need is some clarification.
As a matter of fact, you cannot pass a variable. You can pass only it's value.
Also, one cannot "pass" anything from PHP to javascript. Javascript being generated by PHP. It is like HTML. You just generate any code you want. And you can use any variables, of course, with this code generation.

Your second example will work too, but you need to echo the value of the PHP variable to the page so that JavaScript can read from it. Also use htmlspecialchars to make sure you don't end up with invalid html.

<input type="hidden" value="<?php echo htmlspecialchars($page_id, ENT_QUOTES) ?>" id="page_id" />

You will find your answer in this question.

本文标签: Get php variable within javascriptStack Overflow