-1

my php code get_marker_connect2.php

<?php
$servername = "localhost";
$username = "root";
$passcode = "";
$dbname = "complaint_system";
$con=mysqli_connect($servername,$username,$passcode,$dbname);
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

//store query
$result = mysqli_query($con,"SELECT l_longitude,l_latitude FROM register_complain");

echo "<table border='1'>

<tr>
<th>Latitude</th>
<th>Longitude</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['l_longitude'] . "</td>";
    echo "<td>
    " . $row['l_latitude'] . "</td>";
    echo "</tr>";
}
echo "</table>";
header('Location:http://localhost/cca/View_Map.html');
mysqli_close($con);
?> 

This is my javascript named as javascript1.js

function initialize() {    
    var myLatLng = new google.maps.LatLng(24.86, 67.01);
    var mapOptions = {

    center: myLatLng,
    zoom: 14,
    mapTypeControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP};

    alert('alert array here');

    /*for (var i = 0; i < latlong1.length; i++) {
    alert(" Longitude = ". latlong1[i].longitude);
    alert(" Latitude = ". latlong1[i].latitude);
    }
    */

    map = new google.maps.Map(document.getElementById("view-map"),
        mapOptions);
    var marker = new google.maps.Marker({
    position: simple,
    map: map,
    title: 'Hello Karachi!'});  
}
google.maps.event.addDomListener(window, 'load', initialize);

can i move php array $row['l_longitude'] and $row['l_latitude'] directly to my javascript? i have google it alot but have not found solution yet. i want to alert that array in my javascript at "alert array here"

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
raja121
  • 1,129
  • 2
  • 8
  • 16
  • 1
    *can i move php array... directly to my javascript?* Sure you can. There's lots of literature on this. What have **you** done so far to solve this problem? – esqew Jul 10 '14 at 17:32
  • no in that question there it is passing a single variable here $row is getting an array so its not duplicate. – raja121 Jul 10 '14 at 17:40
  • echo "
    "; echo ""; echo ""; echo "
    "; well i have tried this on my php code
    – raja121 Jul 10 '14 at 17:41

1 Answers1

0

If you wish to print a complex structure into your page that is worked in a browser, you need a syntax that Javascript can parse, and the serverside language can print.

The neareset possibilities are:

  • XML
  • JSON

I recommend JSON for now.

So with PHP, you write somewhere:

echo "var myTransportedArrayJson = \"" . json_encode($my_array) . "\"";

And then let the Javascript parse your json expression:

var myTransportedArray = JSON.parse(myTransportedArrayJson);
alert(myTransportedArray);   // <<< and here you are.

Of course the snippet must be part of the response, otherwise it won't reach the server. Also, you should embrace the parsing with try .. catch. If you go on and work with even more complex structures, you have to consider escaping certain chars. So you will find now some literature :-)

peter_the_oak
  • 3,529
  • 3
  • 23
  • 37
  • 'while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['l_longitude'] . ""; echo " " . $row['l_latitude'] . ""; echo ""; echo $ab = $row['l_latitude']; echo $ab2 = $row['l_longitude']; echo json_encode( $ab );' and in my javascript external i have added var db_array = ; alert(db_array); it is not showing anything – raja121 Jul 11 '14 at 15:46
  • Unfortunately, I can't debug your work in detail. But these questions may help you to clarify: 1) Are you sure all snippets are sent to the browser? 2) What does the code look like in the browser if you watch the source code? Are there mistakes, typos? 3) Start the debugging console of the browser. Do you see exceptions? Do you see unexpected text if you inspect the HTML DOM elements? 4) If all code is there, make sure nothing is started too early. E.g. sometimes, you should wrap code in `$(document).ready( ... );` - Just go on, you'll do it :-) – peter_the_oak Jul 12 '14 at 06:16