Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate PHP <input> field from JavaScript with a GMaps Marker position
    primarykey
    data
    text
    <p>I've managed to add markers to a Google Map on click, but now I need to update an <code>&lt;input&gt;</code> field inside a form each time a new marker is placed. So, when a new marker is placed, the input in question is updated with the latitude and longitude of the marker.</p> <p>I'm using Google Maps Javascript API v3.</p> <p>Here's my Javascript:</p> <pre><code>&lt;script type="text/javascript"&gt; function load() { var map = new google.maps.Map(document.getElementById("map"), { center: new google.maps.LatLng(39.47860556892209,-3.328857421875), zoom: 7, mapTypeId: 'roadmap' }); var infoWindow = new google.maps.InfoWindow; google.maps.event.addListener(map, 'click', function(event) { //NEITHER OF THESE 2 OPTIONS WORK cambiarMarker(event.latLng); //document.getElementById("lat").value = event.latLng.lat(); //document.getElementById("lng").value = levent.latLng.lng(); document.forms['insertData'].lati.value = location.lat(); document.forms['insertData'].long.value = location.lng(); }); var markerActual; function cambiarMarker(location) { //CREATES AND PUT THE NEW MARKER } downloadUrl("phpsqlajax_genxml2.php", function(data) { //GETS INFO FROM AN XML THAT IS CREATED FROM THE DATABASE WITH PHP } function bindInfoWindow(marker, map, infoWindow, html) { //BINDS INFORMATION WINDOW WITH THE XML INFO } function downloadUrl(url, callback) { //GET THE XML FILE } function parseXml(str) { //PARSES FROM THE XML } function doNothing() {} &lt;/script&gt; </code></pre> <p>And here's my HTML:</p> <pre><code>&lt;body onload="load()"&gt; Aqui debería salir algo &lt;div id="map" style="width: 500px; height: 300px"&gt;&lt;/div&gt; &lt;form name="insertData" action="insertar_datos.php" method="post"&gt; Nombre: &lt;input type="text" name="name"&gt;&lt;br&gt; Dirección: &lt;input type="text" name="address"&gt;&lt;br&gt; Latitud: &lt;input type="text" name="lati" id="lat"&gt;&lt;br&gt; &lt;!--HERE'S INPUT1-LATITUDE--&gt; Longitud: &lt;input type="text" name="long" id="lng"&gt;&lt;br&gt; &lt;!--HERE'S INPUT2-LONG--&gt; Teléfono de contacto: &lt;input type="text" name="telefono"&gt;&lt;br&gt; Comentario: &lt;textarea name="comentario"&gt;&lt;/textarea&gt;&lt;br&gt; &lt;input type="submit" value="Enviar"&gt; &lt;/form&gt; &lt;/body&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload