I want to update my database when i click the label in the infowindow. but i cant seems to update even when it shows that "You have donated" the value that i pass in is dynamic data which means that i have retrieve the value and pass it into the infowinow.

<script type="text/javascript">
function initialize() {
//sg map
    var latlng = new google.maps.LatLng(1.3667, 103.8);
    var myOptions = {
      zoom: 11,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
		};
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
	
<?php
$query = "SELECT * FROM standcharbranch";
$result = $mysqli->query($query);
	while($branch = $result->fetch_array())
	{
	$amt = $branch['NumOfDonate'];
	$totalAmt = $amt*0.05;

?>	

//location marker	
	var marker = new google.maps.Marker({
	position: new google.maps.LatLng(<?php echo $branch['latitude'];?>,<?php echo $branch['longtitude'];?>),
	map: map,
	title: "<?php echo $branch['Location'];?>",
	icon: "./Images/sclogo.png"
	});

	var infowindow = new google.maps.InfoWindow();
	//Add an onclick event to the marker that will load the overlay.
	google.maps.event.addListener(marker, 'click', function() {

[B]Here is the label to click for the update[/B]

	var contentString = "<?php echo $branch['Location'].'<br/><br/>'.'$'.$totalAmt.'<br/><br/>'.'<label id=donateCharity onclick=charityDonate()>Donate</label>'?>";
	
	  infowindow.setContent(contentString);
      infowindow.open(map,this);

	  });

<?php
}

?>
}



function charityDonate(){

<?php
[B]Here is the problem[/B]

// construct your SQL statement
$sql = "UPDATE standcharbranch SET NumOfDonate=".($branch['NumOfDonate']+1)." WHERE Location='".$branch['Location']."'";
$checkresult=$mysqli->query($sql);
if($branch['Location']!=""){?>
alert("You have donated");

<?php
}
else if(!$checkresult){
?>
alert("Failed to donated");
<?php
}
$mysqli->close();
?>
}


</script>

Recommended Answers

Are you trying to execute PHP code in a javascript function ? This is not possible, unless you use AJAX.

Jump to Post

I think that's what you are doing. PHP runs server side, so it cannot execute when a Javascript function triggers. The only way to do this is using AJAX. That means that your Javascript function calls a PHP file on the server, and uses it's result.

Jump to Post

All 5 Replies

Are you trying to execute PHP code in a javascript function ? This is not possible, unless you use AJAX.

Are you trying to execute PHP code in a javascript function ? This is not possible, unless you use AJAX.

thanks for the reply..
i encaps it with the <?php ?> script tag also wont work??? i use php to retrieve and pass the value into the infowindow. is this consider as pasing php code into javascript function???

i have modify the code. and it update but update only when i refresh the page. not when i click on the label...

I think that's what you are doing. PHP runs server side, so it cannot execute when a Javascript function triggers. The only way to do this is using AJAX. That means that your Javascript function calls a PHP file on the server, and uses it's result.

I think that's what you are doing. PHP runs server side, so it cannot execute when a Javascript function triggers. The only way to do this is using AJAX. That means that your Javascript function calls a PHP file on the server, and uses it's result.

do you mean use ajax to load the content in the infowindow or update using ajax way? is there any example or can u guide me ? cux i m still new to ajax...

Check the w3schools ajax tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.