将变量从php传输到js [英] transfer a Variable from php to js
本文介绍了将变量从php传输到js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试将变量从php传输到js时,我感到非常困惑.
当我尝试使用id来获取价值时,一切都很好
例如:
I got really confused when i tried to transfer variables from php to js.
everything is fine when i try to get value using an id
for example:
var name = $("#name").val();
但是我的问题是,例如,如果要转换此变量:
but my question is, if i want to convert for example this variable:
$id = 182938; //the id of the user
此变量:
var id;
这个问题可能是愚蠢的……也许对您来说很容易,但对我而言却不是:p
我查了一下,只在我要找的地方找到了这样的东西:
this question might be dumb... maybe easy for you, but not for me :p
I have looked it up and only found something like this anywhere i looked for:
<?php
$number = rand();
?>
<script language="javascript">
var num = <?php echo $number ?>
</script>
它与传递值一样有影响吗?
Does it have the same affect as passing down the value?
谢谢!
推荐答案
不太正确.编码为JSON将确保它是有效的JavaScript文字.
Not quite. Encoding as JSON will ensure that it is a valid JavaScript literal.
var num = <?php echo json_encode($number) ?>
这篇关于将变量从php传输到js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文