0
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 must contain the value of location passed by php every tym the code loops` but why it is not actually passing any data?

jS file
 var locate="<?php echo $location?;>";
 alert (locate);
 $(".weather").html(html);   

2 Answers2

2

The problem is you are saving the whole file in .js format where the php code you write is not parsed.

Instead you can do this just set the variable in the .php file where it can be excecuted like

PHP File

window.locate="<?php echo $location?;>";

and now use that variable in js file

alert (window.locate);
Phpstall
  • 56
  • 6
  • still not working your code instead prints the value i just want to pass the $location value to my js so that i can put the $location in my other location variable of js – Anuj Shrestha Dec 01 '16 at 11:16
2

From js file one cannot access php variable.

You can do this from you php file and access locate in your js file

<script>
    var locate = <?php echo $location ?>;
</script>