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.
更多推荐
发布评论