在JavaScript中获取PHP变量

编程入门 行业动态 更新时间:2024-10-23 15:33:55
本文介绍了在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变量

本文发布于:2023-10-31 16:29:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   JavaScript   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!