views:

21

answers:

1

Hey,

Just a follow up from a previous question. I've got the following form..

     <body onload="initialize()" onunload="GUnload()">

     <form action="welcome.php" method="post">
     Name: <input type="text" name="fname" /><br/>
     <br/>Description: <BR>
     <TEXTAREA NAME="description" COLS=40 ROWS=6></TEXTAREA><br/>
     <div id="map_canvas" style="width: 500px; height: 300px"></div><br/>
     Latitude:<INPUT NAME="lat" SIZE=20 MAXLENGTH=40
     onKeyPress="return numbersonly(this, event)"><br/>
     Longitude:<INPUT NAME="lng" SIZE=20 MAXLENGTH=40
     onKeyPress="return numbersonly(this, event)">
     <input type="submit" />
     </form>

     </body>

and the following Google API javascript...

 <script type="text/javascript">

 function initialize() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(53.347247,-6.259031), 13);
    map.setUIToDefault();

    GEvent.addListener(map, "click", function(overlay, latLng)
{
    // display the lat/lng in your form's lat/lng fields
    document.getElementById("lat").value = latLng.lat();
    document.getElementById("lng").value = latLng.lng();
});
  }
 }


  </script>

Basically I'm looking to put the lat and long value of a location in the lat + lng fields of the form but it doesn't seem to be copying.. why? what am I doing wrong?

+1  A: 

If using document.getElementById(), Your input elements have to have an id of "lat" and "lng", along with having a name of lat and long for when you pass the form.. i.e.:

<input type='text' id='lat' name='lat'>

You can think of the id as what the DOM uses to identify the element, and the name, as what the browser passes in a GET/POST.

MarkD