newbie: Ajax & Google Map & database

J

Jeff

Hey

I'm having problem with the code below, it don't update the database. I've
posted both the javascript and the php script, but I think the error is in
the javascript below. When I click in the map a new marker is placed exactly
where I clicked in the map, then a alert displays the url I'm sending in
request.open... the url is okay... Maybe it's because I didn't set a
function to request.onreadystatechange??

JavaScript part:
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(63.4415, 10.4289 ), 13);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());

GEvent.addListener(map, "click", function(marker, point) {
map.clearOverlays();
var marker = new GMarker(point);
map.addOverlay(marker);

var request = GXmlHttp.create();
var url = "index.php?mode=loc&opt=3&id=4";

url = url + "&lng=" + point.lng();
url = url + "&lat=" + point.lat();

alert(url);
request.open("GET", url, true);
});

}
}

This is the PHP (version 5.2.0) code:
<?php
$lattitude = $_POST["lattitude"];
$longitude = $_POST["longitude"];
@include("config.php");
@ $db = new mysqli($host, $user, $dbpwd, $database);

if (!empty($lattitude) && !empty($longitude)) {
$id = $_POST["id"];
$sql = "update Location set Lattitude = " . $lattitude . ", Longitude = "
.. $longitude . " where Id = " . $id;
$result = $db->query($sql);

$db->close();

}
?>

any suggestions?

jeff
 
J

Jeff

Jeff said:
Hey

I'm having problem with the code below, it don't update the database. I've
posted both the javascript and the php script, but I think the error is in
the javascript below. When I click in the map a new marker is placed
exactly where I clicked in the map, then a alert displays the url I'm
sending in request.open... the url is okay... Maybe it's because I didn't
set a function to request.onreadystatechange??

JavaScript part:
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setCenter(new GLatLng(63.4415, 10.4289 ), 13);
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());

GEvent.addListener(map, "click", function(marker, point) {
map.clearOverlays();
var marker = new GMarker(point);
map.addOverlay(marker);

var request = GXmlHttp.create();
var url = "index.php?mode=loc&opt=3&id=4";

url = url + "&lng=" + point.lng();
url = url + "&lat=" + point.lat();

alert(url);
request.open("GET", url, true);
});

}
}

This is the PHP (version 5.2.0) code:
<?php
$lattitude = $_POST["lattitude"];
$longitude = $_POST["longitude"];
@include("config.php");
@ $db = new mysqli($host, $user, $dbpwd, $database);

if (!empty($lattitude) && !empty($longitude)) {
$id = $_POST["id"];
$sql = "update Location set Lattitude = " . $lattitude . ", Longitude =
" . $longitude . " where Id = " . $id;
$result = $db->query($sql);

$db->close();

}
?>

any suggestions?

jeff

Well, I've found some errors in my code above:
request.send(null); as missing, I've added it:

request.open("GET", url, true);
request.send(null);

and also there was a mismatch between the parameter names in javascript and
php: a parameter was called "lat" in javascript and "lattitude" (also solved
the mismatch on longitude) in php... these errors are now solved.. But still
got the same problem, the database isn't updated...

Any suggestions?

Jeff
 
J

Jeff

In the javascript i used GET, but in the PHP code I used POST... solved that
and the problem was solved
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top