PHP内部的JavaScript代码(PHP code inside javascript)

编程入门 行业动态 更新时间:2024-10-26 23:35:38
PHP内部的JavaScript代码(PHP code inside javascript)

我想看看如何获​​取$ CITY,$ STATE和$ count数据以包含在JavaScript代码中?

这是非工作代码:

<?php echo('<script type="text/javascript"> var address$count = $CITY$STATE');?> var map = new google.maps.Map(document.getElementById('map'), { mapTypeId: google.maps.MapTypeId.TERRAIN, zoom: 6 }); var geocoder = new google.maps.Geocoder(); geocoder.geocode({'address': address<?php echo($count);?>}, function(results, status) { if(status == google.maps.GeocoderStatus.OK) { new google.maps.Marker({ position: results[0].geometry.location, map: map }); map.setCenter(results[0].geometry.location); } }); </script> <?php } ?> </body> </html>

谢谢您的帮助!

I would like to see how can I get the $CITY, $STATE, and $count data to be included in the javascript code ?

Here is the non working code:

<?php echo('<script type="text/javascript"> var address$count = $CITY$STATE');?> var map = new google.maps.Map(document.getElementById('map'), { mapTypeId: google.maps.MapTypeId.TERRAIN, zoom: 6 }); var geocoder = new google.maps.Geocoder(); geocoder.geocode({'address': address<?php echo($count);?>}, function(results, status) { if(status == google.maps.GeocoderStatus.OK) { new google.maps.Marker({ position: results[0].geometry.location, map: map }); map.setCenter(results[0].geometry.location); } }); </script> <?php } ?> </body> </html>

Thanks for the help!

最满意答案

如果你的变量是正确的,那么javascript通常需要引用字符串,如下所示:

<?php echo '<script type="text/javascript"> var address'.$count.' = "'.$CITY.$STATE.'"'; ?>

另外,要在一个字符串中使用PHP变量,需要双引号,所以如果你打算保留单引号,你需要使用双引号。

If your variables are correct, javascript usually needs quotes around strings, like so:

<?php echo '<script type="text/javascript"> var address'.$count.' = "'.$CITY.$STATE.'"'; ?>

Also, to use PHP variables inside a string would require double quotes, so if you intend to keep the single quotes you would need to concentenate.

更多推荐

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

发布评论

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

>www.elefans.com

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