map page becomes blank after inserting php code

2019-06-14 16:21发布

问题:

My gmap page turns blank after inserting php code into the infowindow content.

google.maps.event.addListener(noi, 'mouseover', function() {
  var infowindow = new google.maps.InfoWindow({
    content: '<?php 
        if ($count==0){
        echo "No Open Tickets";
        }
    else{
        echo "<table>";
        foreach ($NOIcompliancearray as $SLA_Compliance=>$count) {
            $Npath = $Nimages[$SLA_Compliance];
            echo "<tr>";
            echo "<td><a href='city.php?city=Noida&compliance=".$SLA_Compliance."'><img src='IndiaImages/".$Npath."' title='".$SLA_Compliance."' ></td>";
            echo "<td>".$count."</td>";
            echo "</tr>";
            }
        echo "</table>";        
    }
  ?>'
    size: new google.maps.Size(100,100),
  });
google.maps.event.addListener(noi, 'mouseover', function() {
infowindow.open(map,noi);
setTimeout(function() { infowindow.close(map, noi) }, 5000);
});

If i replace php code with some static content it works fine. Also, when I tried opening the webpage source code, it gives me result which i wanted to see in the info window. I am not sure where I am making mistake.

output from the webpage source code: content:

'<table><tr><td><a href='city.php?city=Noida&compliance=A'><img src='IndiaImages/Circle_Red.gif' title='A' ></td><td>3</td></tr><tr><td><a href='city.php?city=Noida&compliance=C'><img src='IndiaImages/Circle_Yellow.gif' title='C' ></td><td>10</td></tr></table>Noida'

Kindly help me understand the mistake and to mitigate the problem.

回答1:

Look at the function

google.maps.event.addListener(noi, 'mouseover', function() {
  var infowindow = new google.maps.InfoWindow({
    content: '...'
    size: new google.maps.Size(100,100),
  });

with php you get

google.maps.event.addListener(noi, 'mouseover', function() {
  var infowindow = new google.maps.InfoWindow({
    content: '<table>
            <tr>
            <td><a href='
   // 'city.php?city=......' is after content : string ending sign `'` will be ignored by function !
    size: new google.maps.Size(100,100),
  });

The first ' generated by php will interpreted as end of the string the rest will be in html source. you can see it but ignored by the function.

So don't use ' in that kind of code.

echo "<td><a href='city.php?city=Noida&compliance=".$SLA_Compliance."'>

escape the " sign instead

echo "<td><a href=\"city.php?city=Noida&ompliance=\"".$SLA_Compliance."\">"

escape " look here