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);   
1

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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
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>

1 Comment

nothing is working. may be it could be because i am coding for wordpress themeing??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.