在javascript中加载php变量 [英] Load php variable in javascript
本文介绍了在javascript中加载php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新手
如何在javascript中加载变量php?
how to load variable php in javascript?
这是我的脚本(弹出窗口。 php)
this is my script (popup.php)
<?php $id_user = $this->session->userdata('id'); ?>
<script type="text/javascript">rules: {
user: {
required: true,
number: true,
range: [1, 8]
} <script>
示例变量。
$ id_user = 10;
如何输入 id_user
到范围:[1,8]
所以范围输出范围:[1,10]
我的英文不好
非常感谢你
BR
Puja
推荐答案
尝试
<script type="text/javascript">
var $uid = <?php echo json_encode($this->session->userdata('id')); ?>;
rules: {
user: {
required: true,
number: true,
range: [1, $uid]
}
<script>
为什么 json_encode
?它将您传递给它的值转换为有效的JSON - 引用字符串,转义引号和其他特殊字符等。没有它,只要您的echoed变量包含除简单字母数字字符串之外的其他内容,您基本上就会中断。
Why json_encode
? It turns the value you pass to it into valid JSON - quotes the strings, escapes quotes and other special chars, etc. Without it, you're basically going to break whenever your echoed variable contains something other than a simple alphanumeric string.
这篇关于在javascript中加载php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文