Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Help with PHP/MYSQL

Links for php scripts

Moderators: macek, egami, gesf

Help with PHP/MYSQL

Postby dkar » Sun Sep 09, 2012 8:23 am

I just started learning php and I face some problems with my code. I am trying to read coordinates from a table in a database and display them as markers on a google map. I did this but then I want to have a drop down menu and by selecting a specific value (eg activity = climbing) - the corresponding marker should appear.
I have two separate files of code. In the php code, in the sql query (in red) if I have in the WHERE clause: name='$category' the code returns nothing. If I put a specific value from the table (like name='restaurant') then it works fine. So, it seems that it can not get the value from the dropdown menu which is in the second file. It must be something very simple that I am missing here. Some logical mistake I guess. Any ideas?
Why I can not get the value of the dropdown menu?

Code: Select all

$category = $_POST['category'];

function parseToXML($htmlStr)
return $xmlStr;

// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $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
[color=red]$query = "SELECT * FROM markers WHERE name='$category'";[/color]
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());

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

// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = mysql_fetch_assoc($result)){
  echo '<marker ';
 echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo '/>';

// End XML file
echo '</markers>';



Code: Select all
!DOCTYPE html >
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>PHP/MySQL & Google Maps Example</title>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">

    var customIcons = {
      restaurant: {
        icon: '',
        shadow: ''
      bar: {
        icon: '',
        shadow: ''

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(47.6145, -122.3418),
        zoom: 13,
        mapTypeId: 'roadmap'
      var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP file
      downloadUrl("markers.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
          var html = "<b>" + name + "</b> <br/>" + address;
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow
          bindInfoWindow(marker, map, infoWindow, html);

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);, marker);

    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);
      };'GET', url, true);

    function doNothing() {}




    <div id="map" style="width: 500px; height: 300px"></div>
[color=red] <label> choose activity:
       <select name="category">
        <option value="Pan Africa Market">Pan Africa Market</option>
        <option value="Golf">Golf</option>
        <option value="Hiking">Hiking</option>
    <input type="submit" onClick="load()">


New php-forum User
New php-forum User
Posts: 2
Joined: Sun Sep 09, 2012 8:02 am

Re: Help with PHP/MYSQL

Postby freshnet » Sun Sep 09, 2012 3:19 pm

You don't have a submit button inside your form so nothing will ever get submitted.
User avatar
php-forum Active User
php-forum Active User
Posts: 278
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.