在JavaScript中获取PHP变量 [英] Get php variable within javascript

查看:45
本文介绍了在JavaScript中获取PHP变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有趣的问题.我目前正在开发php页面,需要在javascript onload中访问一个php变量.

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" />

如果同时打开两个视图,我将无法像这样获得唯一的页面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();

因为存在两个具有相同输入ID的视图,这些视图不是唯一的.长话短说,在JavaScript中引用php变量是否有效?

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?

推荐答案

长话短说对引用一个php变量javascript.

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

简短的回答,是的,您可以... PHP是服务器端语言,可以在需要的地方使用它.

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

注意::我假设您正在使用扩展名为 php 的文件中进行此操作.

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

这篇关于在JavaScript中获取PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆