var marker,i,marker1,k;
for(i=0; i <20; i++)
{
//I want to assign this i variable value in php variable
function addMarker(latLng, map) {
var marker = new google.maps.Marker({
position: latLng,
map: map,
animation:google.maps.Animation.BOUNCE
});
//like as
var conte='<?php $j="<script>document.write(i);</script>";
?>';
I want to use that value below here:
var contentvalue= '<div style="color: #008000;"><strong><b><?php echo "$b[$j]" ?></b></strong></div>';
var infowindow = new google.maps.InfoWindow({
content: contentvalue,
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
return marker[i];
}
}
Essentially, you are trying to do this backwards. You can use PHP to dynamically build a JavaScript block on the server side but, you cannot use PHP while JavaScript is executing. Since PHP is Server-side and JS is Client-side, you need to decide to go with one of the languages.
Also, you shouldn't be defining a function inside of your for loop. Just define the function outside and call it from within the for loop.
function addMarker(latLng, map) {
var marker = new google.maps.Marker({
position: latLng,
map: map,
animation:google.maps.Animation.BOUNCE
});
return marker;
}
function addInfoBox(contentString) {
var infowindow = new google.maps.InfoWindow({
content: contentString
});
return infowindow;
}
var i = 0
for(i=0; i < 20; i++)
{
var contentString='<script>document.write('+i+');</script>';
var marker = addMarker(latLng, map);
var info = addInfoBox(contentString, marker);
marker.addListener('click', function() {
info.open(map, marker);
});
}
This code is not at all perfect but, I think it gives a general idea of how you should start here. You'll have to clean it up and make sure it fits in with your application. I do not see a need for any PHP here as this is all client-side code and Google Maps API does not parse or execute any PHP code.
Try :
<script>
var phparr = <?php echo json_encode($phparr); ?>;
for (i in phparr){
alert("hey" + phparr[i]);
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.