[英]Passing variable from Php to js?
I am trying to send MY location to my js file. The location have the gives correct value when it is echoed from php. But when i try to access it from my js it does not access the correct value instead contains the value `<?php echo $location;?>` well how can i access the value in js?
PHP
while($loop->have_posts()):$loop->the_post();?>
<?php $location=get_field('location');?>
<div class="col-sm-15">
<div class="widget-box">
<div class="weather"></div>
</div>
</div>
<?php
endwhile;
在代码循环时,locate必须包含php每次传递的location值,但是为什么它实际上没有传递任何数据呢?
jS file
var locate="<?php echo $location?;>";
alert (locate);
$(".weather").html(html);
问题是您将整个文件保存为.js格式,其中您编写的php代码未解析。
取而代之的是,您只需在.php文件中设置变量即可执行操作,例如
window.locate="<?php echo $location?;>";
现在在js文件中使用该变量
alert (window.locate);
从js文件中无法访问php变量。
您可以从php文件中执行此操作,然后在js文件中访问定位
<script>
var locate = <?php echo $location ?>;
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.