0
                <script type="text/javascript">
                    var places = [];
                </script>
                <?php
                $date = $_POST['orderdate'];
                $file = fopen("http://www.spc.noaa.gov/climo/reports/".$date."_rpts_hail.csv", "r");
                $content = fgetcsv($file, 1000, ",");
                $id = 1;
                while (($content = fgetcsv($file, 1000, ",")) !== FALSE) {
/*******************************************************************************************
                    Values of content
                    (ignore)****content[0] = Time*******(ignore)
                                content[1] = Size
                    (ignore)****content[2] = Location***(ignore)
                                content[3] = City
                                content[4] = State
                                content[5] = Lat 
                                content[6] = Long
                                content[7] = Comments
*******************************************************************************************/
                    if ($content !== false) {
                    ?>
                        <script type="text/javascript">                             
                        places.push(new google.maps.LatLng(<?php echo json_encode($content[5]); ?>, <?php echo json_encode($content[6]); ?>));
                        </script>
                    <?php           
                    }

Is there any way that when a page loads, some php code will run and get an array of information and then have that information be put into a javascript variable to use?

1 Answer 1

8

Yes.

Use for example json_encode():

<?php 
   $array = array("first" => 1, "second" => 2, "third" => 3, "fourth" => 4);
 ?>

 <script>
   var my_array = <?php echo json_encode($array); ?>
   alert(my_array[1]); // alerts "first"
 </script>
Sign up to request clarification or add additional context in comments.

7 Comments

I just linked the function to it's reference page. I decided to look it up, so I made it convenient for other viewers to look up. Want me to roll it back? Your call.
I updated my code. I really need to know how to push the php data into javascript
@Surreal ah, we did the same thing at the same time then (I edited it heavily during the 5-minute window). Then everything's fine
@shin I don't understand - aren't you already doing the very thing?
@pekka Sorry I just fixed the problem. I did use your function but I was trying to put two values together. I put the new code that works up above. Thanks for the help
|

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.