Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6370511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:56:21+00:00 2026-05-25T00:56:21+00:00

I am using the PHP and AJAX coding below to populate a map showing

  • 0

I am using the PHP and AJAX coding below to populate a map showing various markers for a given location stored in a mySQL database.

The markers are correctly shown but what I would like to be able to do is to populate the fields on my form with the associated data from the database, so that as each marker is clicked the fields will show the data pertient to that marker.

PHP Code

<
?php 
require("phpfile.php"); 

// Start XML file, create parent node 

$dom = new DOMDocument("1.0"); 
$node = $dom->createElement("markers"); 
$parnode = $dom->appendChild($node); 

// Opens a connection to a MySQL server 

$connection=mysql_connect ("hostname", $username, $password); 
if (!$connection) { die('Not connected : ' . mysql_error());} 

// Set the active MySQL database 

$db_selected = mysql_select_db($database, $connection); 
if (!$db_selected) { 
die ('Can\'t use db : ' . mysql_error()); 
} 

// Select all the rows in the markers table 

$query = "SELECT findid, locationid, findosgb36lat, findosgb36lon, dateoftrip, findcategory, findname, finddescription, pasref, findimage, additionalcomments FROM finds WHERE `locationid` = '2'"; 

$result = mysql_query($query); 
if (!$result) { 
die('Invalid query: ' . mysql_error()); 
} 

header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each 

while ($row = @mysql_fetch_assoc($result)){ 
// ADD TO XML DOCUMENT NODE 
$node = $dom->createElement("marker"); 
$newnode = $parnode->appendChild($node); 
$newnode->setAttribute("findid",$row['findid']); 
$newnode->setAttribute("locationid",$row['locationid']); 
$newnode->setAttribute("findosgb36lat",$row['findosgb36lat']);
$newnode->setAttribute("findosgb36lon",$row['findosgb36lon']);
$newnode->setAttribute("dateoftrip",$row['dateoftrip']); 
$newnode->setAttribute("findcategory",$row['findcategory']); 
$newnode->setAttribute("findname",$row['findname']); 
$newnode->setAttribute("finddescription",$row['finddescription']); 
$newnode->setAttribute("pasref",$row['pasref']);
$newnode->setAttribute("findimage",$row['findimage']);
$newnode->setAttribute("additionalcomments",$row['additionalcomments']);

} 

echo $dom->saveXML(); 

?>

HTML Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Finds Per Location</title>
        <link rel="stylesheet" href="css/findsperlocationstyle.css" type="text/css" media="all" />
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
        <script type="text/javascript"> 
            var customIcons = {
            Artefact: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            },
            Coin: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            },
            Jewellery: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            }
            };

            // Creating a LatLngBounds object
            var bounds = new google.maps.LatLngBounds();

            function load() { 
            var map = new google.maps.Map(document.getElementById("map"), { 
            center: new google.maps.LatLng(54.312195845815246,-4.45948481875007), 
            zoom:14, 
            mapTypeId: 'satellite' 
            }); 

            // Change this depending on the name of your PHP file 
            downloadUrl("phpfile.php", function(data) { 
            var xml = data.responseXML; 
            var markers = xml.documentElement.getElementsByTagName("marker"); 
            var bounds = new google.maps.LatLngBounds();
            for (var i = 0; i < markers.length; i++) { 
            var findid = markers[i].getAttribute("findid");
            var locationid = markers[i].getAttribute("locationid"); 
            var dateoftrip = markers[i].getAttribute("dateoftrip");
            var findcategory = markers[i].getAttribute("findcategory");
            var findname = markers[i].getAttribute("findname");
            var finddescription = markers[i].getAttribute("finddescription");
            var pasref = markers[i].getAttribute("pasref");
            var findimage= markers[i].getAttribute("findimage");
            var additionalcomments= markers[i].getAttribute("additionalcomments");
            var point = new google.maps.LatLng( 
            parseFloat(markers[i].getAttribute("findosgb36lat")), 
            parseFloat(markers[i].getAttribute("findosgb36lon")));
            var icon = customIcons[findcategory] || {}; 
            var marker = new google.maps.Marker({          
            map: map, 
            position: point,
            title: 'Click to view details',
            icon: icon.icon,
            shadow: icon.shadow
            }); 
            bounds.extend(point);
            map.fitBounds(bounds);
            } 
            }); 
            } 

            function downloadUrl(url, callback) { 
            var request = window.ActiveXObject ? 
            new ActiveXObject('Microsoft.XMLHTTP') : 
            new XMLHttpRequest; 

            request.onreadystatechange = function() { 
            if (request.readyState == 4) { 
            request.onreadystatechange = doNothing; 
            callback(request, request.status); 
            } 
            }; 

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

            function doNothing() {} 

            </script> 
</head> 
            <body onLoad="load()">
            <form name="findsperlocation" id="findsperlocation">
              <p align="left"><label>Location id<br />
              </label>
              </p>
              <div>
                <div align="left">
                  <input name="locationid" type="text" id="locationid" value="2" readonly="readonly"/>
                </div>
              </div>
              <p align="left"><label>Date of Trip<br />
              </label>
              </p>
              <div>
                <div align="left">
                  <input name="dateoftrip" type="text" id="dateoftrip" readonly="readonly"/>
              </div>
              </div>
              <p align="left">
                <label></label>
                <label>Find Category</label>
              </p>
              <div>
                <div align="left">
                  <input name="findcategory" type="text" id="findcategory" size="10"readonly="readonly"/>
              </div>
              </div>
              <p align="left">
                <label>Find Name</label>
              </p>
              <div>
                <div align="left">
                  <input name="findname" type="text" id="findname" size="35" readonly="readonly"/>
                </div>
              </div>
              <p align="left"><label>Find Description</label>&nbsp;</p>
              <div>
                <div align="left">
                  <input name="finddescription" type="text" id="finddescription" size="100"readonly="readonly"/>
                </div>
              </div>
                <p align="left">
                <label>
                <label>PAS Ref.  </label>
              </p>
              <div>
                <div align="left">
                  <input name="pasref" type="text" id="pasref" readonly="readonly"/>
                </div>
              </div>
              <p align="left"><label>Additional Comments</label>
              </p>
              <div>
                <div align="left">
                  <textarea name="additionalcomments" cols="50" rows="12" id="additionalcomments" readonly="readonly"></textarea>
                </div>
              </div>
              <p align="left"><br />  
                </label>
              </p>
              <div>
                <div align="left"></div>
              </div>
            </form>
            <div id="map"></div>
            </body> 
</html>

I think I’m half way there because I’m mangaing to pull all of the information from the database. I can see this when I run the php script in my web browser, but I’m just not sure what to do for the next step.

What do I need to do next?

UPDATED CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Finds Per Location</title>
        <link rel="stylesheet" href="css/findsperlocationstyle.css" type="text/css" media="all" />
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script>
        <script type="text/javascript"> 
            var customIcons = {
            Artefact: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            },
            Coin: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            },
            Jewellery: {
            icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
            shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
            }
            };

            // Creating a LatLngBounds object
            var bounds = new google.maps.LatLngBounds();

            function load() { 
            var map = new google.maps.Map(document.getElementById("map"), { 
            center: new google.maps.LatLng(54.312195845815246,-4.45948481875007), 
            zoom:14, 
            mapTypeId: 'satellite' 
            }); 

            // Change this depending on the name of your PHP file 
            downloadUrl("phpfile.php", function(data) { 
            var xml = data.responseXML; 
            var markers = xml.documentElement.getElementsByTagName("marker"); 
            var bounds = new google.maps.LatLngBounds();
            for (var i = 0; i < markers.length; i++) { 
            var findid = markers[i].getAttribute("findid");
            var locationid = markers[i].getAttribute("locationid"); 
            var dateoftrip = markers[i].getAttribute("dateoftrip");
            var findcategory = markers[i].getAttribute("findcategory");
            var findname = markers[i].getAttribute("findname");
            var finddescription = markers[i].getAttribute("finddescription");
            var detectorname = markers[i].getAttribute("detectorname");
            var searchheadname = markers[i].getAttribute("searchheadname");
            var detectorsettings = markers[i].getAttribute("detectorsettings");
            var pasref = markers[i].getAttribute("pasref");
            var findimage= markers[i].getAttribute("findimage");
            var additionalcomments= markers[i].getAttribute("additionalcomments");
            var point = new google.maps.LatLng( 
            parseFloat(markers[i].getAttribute("findosgb36lat")), 
            parseFloat(markers[i].getAttribute("findosgb36lon")));
            var icon = customIcons[findcategory] || {}; 
            var marker = new google.maps.Marker({          
            map: map, 
            position: point,
            title: 'Click to view details',
            icon: icon.icon,
            shadow: icon.shadow,
            formdateoftrip: "dateoftrip",
            formfindcategory: "findcategory"
            }); 
            bounds.extend(point);
            map.fitBounds(bounds);
            } 
            google.maps.event.addListener(marker, "click", function() {   alert("Associated data: " + this.formdateoftrip + ", " + this.findcategory); }); 
            }); 
            } 


            function downloadUrl(url, callback) { 
            var request = window.ActiveXObject ? 
            new ActiveXObject('Microsoft.XMLHTTP') : 
            new XMLHttpRequest; 

            request.onreadystatechange = function() { 
            if (request.readyState == 4) { 
            request.onreadystatechange = doNothing; 
            callback(request, request.status); 
            } 
            }; 

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

            function doNothing() {} 

            </script> 

</head> 
            <body onLoad="load()">
            <form name="findsperlocation" id="findsperlocation">
              <p align="left"><label>Location id<br />
              </label>
              </p>
              <div>
                <div align="left">
                  <input name="locationid" type="text" id="locationid" value="2" readonly="readonly"/>
                </div>
              </div>
              <p align="left"><label>Date of Trip<br />
              </label>
              </p>
              <div>
                <div align="left">
                  <input name="dateoftrip" type="text" id="dateoftrip" readonly="readonly"/>
              </div>
              </div>
              <p align="left">
                <label></label>
                <label>Find Category</label>
              </p>
              <div>
                <div align="left">
                  <input name="findcategory" type="text" id="findcategory" size="10"readonly="readonly"/>
              </div>
              </div>

              </form>
            <div id="map"></div>
            </script>
            </body> 
</html>

CODE SNIPPET

var marker = new google.maps.Marker({ 
map: map, 
position: point, 
title: 'Click to view details', 
icon: icon.icon, 
shadow: icon.shadow, 
formdateoftrip: "dateoftrip", 
formfindcategory: "findcategory",
formfindname: "findname",
formfinddescription: "finddescription",
formpasref: "pasref",
formfindimage: "findimage",
formadditionalcomments: "additionalcomments"
}); 
bounds.extend(point); 
map.fitBounds(bounds); 
} 
google.maps.event.addListener(marker, "click", function() {  
document.getElementById('dateoftrip').value = this.formdateoftrip;
document.getElementById('findcategory').value = this.formfindcategory; 
document.getElementById('findname').value = this.formfindname
}); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:56:22+00:00Added an answer on May 25, 2026 at 12:56 am

    You can store additional data in your marker just by adding new fields like this:

    var marker = new google.maps.Marker(
    {          
      map : map, 
      position : point,
      title : 'Click to view details',
      icon : icon.icon,
      shadow : icon.shadow,
      myVariable1 : "some data from xml",
      myVariable2 : "some other data"
    }); 
    

    Then all you have to do is register onClick event for the marker and put it’s data into the form.

    google.maps.event.addListener(marker, "click", function()
    {
      alert("Associated data: " + this.myVariable1 + ", " + myVariable2);
    });
    

    // Edit:

    Obviously the code above only shows how to retrieve data from the marker – it was just an example. Putting your data from JavaScript into the form is a 2 step process. The first thing you have to do is to give every field you want to fill an unique id via “id” attribute. You’ve already done it. Then all you have to do is put following code in the onClick event (instead of alert() in the sample above):

    document.getElementById('formdateoftrip').value = this.formdateoftrip;
    // repeat it for other fields here
    

    Good luck 😉

    // Another edit:

    I didn’t notice you’ve put google.maps.event.addListener in wrong place. The reason it works for only one marker is you’ve put it outside your “for” loop which creates the markers. It has to be inside, right after the “map.fitBounds(bounds);” but before “}”, so move it one line up.

    The second problem lies in passing the data in the marker. If you want to reference variables, you can’t put them in quotes. You use quotes to write strings.

    Replace:

    formdateoftrip: "dateoftrip", 
    formfindcategory: "findcategory",
    ...
    

    Into:

    formdateoftrip: dateoftrip, 
    formfindcategory: findcategory,
    // fix the others below too
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to submit information to a mySql database using php and ajax.
I have been working on on an AJAX chat application using php, mysql. It's
I'm coding something in PHP, I'm just using Ajax for the styling. basically I
I have created an form validation using ajax/php. Each text box is validated using
I'm using the W3 Schools example: http://www.w3schools.com/PHP/php_ajax_database.asp to display a table of database data
I am using jQuery and Ajax, and my Ajax.php file returns the following field
I'm trying to create a AJAX push implementation in PHP using a Comet long-polling
I am using jQuery, JavaScript and PHP. My Ajax.php file just displays the data.
I'm posting data using jquery/ajax and PHP at the backend. Problem being, when I
I am using an JavaScript (and AJAX) to dynamically load a PHP page into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.