一尘不染

如何将Php变量值分配给Javascript变量?

php

我正在使用以下代码:

<script type="text/javascript">
<?php $ctnme = $_SERVER['REQUEST_URI'];
$cnme = explode("/",$ctnme);
echo $cname = $cnme[1];
?>
var spge = <?php echo $cname; ?> ;
alert(spge);
</script>

该值不警报。怎么了


阅读 240

收藏
2020-05-29

共1个答案

一尘不染

实质上:

<?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>
2020-05-29