如何将PHP变量值赋给Javascript变量?
问题描述:
Possible Duplicate:
What's the best way to pass a PHP variable to Javascript?
我使用以下代码:
I am using the following code:
<script type="text/javascript">
<?php $ctnme = $_SERVER['REQUEST_URI'];
$cnme = explode("/",$ctnme);
echo $cname = $cnme[1];
?>
var spge = <?php echo $cname; ?> ;
alert(spge);
</script>
该值不会提示。什么是错误?
The value doesn't alert. What is the mistake?
答
实质上:
Essentially:
<?php
//somewhere set a value
$var = "a value";
?>
<script>
// then echo it into the js/html stream
// and assign to a js variable
spge = '<?php echo $var ;?>';
// then
alert(spge);
</script>