1

I am working on a page that displays all state parks. Something like this:

http://www.comehike.com/outdoors/state.php?country_id=1&state_id=6&state_name=California&country_name=

I have the park data, including lat/lng in my database and I already get that data during the request. What I want to avoid is doing an AJAX call which will query the park data again.

Is it possible for me to just send the PHP result set to the JS somehow and to loop through the parks and display them?

I am already doing something like that by sending the map-center lat/lng so I figured it is doable with a result set.

Thanks, Alex

1
  • Could you be more clear? What do you mean "avoid doing an Ajax call again"?? When do you do it the first time and what is the flow of messages? That'll help clarify your question better and help others in answering it Commented Apr 30, 2011 at 16:52

1 Answer 1

2

This depends on how your pages are setup. If you're using some kind of MVC setup, you need to pass the variable to your view first. Or if it's a single page, just retrieve the PHP above and pass it as a variable.

<?php
   $parkData   = $model->getData();  //make sure this returns an array or an object array or something
   $parkDataJson = json_encode($parkData);
?>

//in your view

<script type='text/javascript'>
   let parkData = <?php echo $parkDataJson ?>;
   //now you have a JSON array available for your use
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

That idea worked. Sorry can't vote you up because it requires 15 reputation points lol
@mickmackusa This answer is from 10 years ago, tbh I don't even remember why I added an unnecessary eval in there. Anyway, I've updated it, thanks.

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.