dkar Posted September 9, 2012 Share Posted September 9, 2012 Hi, 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? <?php require("connectmarker.php"); $category = $_POST['category']; function parseToXML($htmlStr) { $xmlStr=str_replace('<','<',$htmlStr); $xmlStr=str_replace('>','>',$xmlStr); $xmlStr=str_replace('"','"',$xmlStr); $xmlStr=str_replace("'",''',$xmlStr); $xmlStr=str_replace("&",'&',$xmlStr); 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)){ // ADD TO XML DOCUMENT NODE 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>'; ?> *************** !DOCTYPE html > <head> <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="http://maps.googleapis.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> //<![CDATA[ var customIcons = { restaurant: { icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png', shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' }, bar: { icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png', shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' } }; 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( parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); 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); infoWindow.open(map, 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); } }; request.open('GET', url, true); request.send(null); } function doNothing() {} //]]> </script> </head> <body> <div id="map" style="width: 500px; height: 300px"></div> <form> [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> </select> </label>[/color] </form> <input type="submit" onClick="load()"> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/268184-help-with-phpmysql/ Share on other sites More sharing options...
cyberRobot Posted September 10, 2012 Share Posted September 10, 2012 It looks like your form goes straight to JavaScript. To use PHP code, the form data needs to be sent back to the server for processing. Quote Link to comment https://forums.phpfreaks.com/topic/268184-help-with-phpmysql/#findComment-1376661 Share on other sites More sharing options...
dkar Posted September 10, 2012 Author Share Posted September 10, 2012 Yes, I was thinking it must be related to this. If I add an action='file.php' on the form tag then I get as a result the xml file. Do you have any idea what I should do? Quote Link to comment https://forums.phpfreaks.com/topic/268184-help-with-phpmysql/#findComment-1376662 Share on other sites More sharing options...
lemmin Posted September 10, 2012 Share Posted September 10, 2012 Without an action attribute set in your form tag, the default value of GET will be used. You are using the $_POST variable in your PHP, though. Adding "action='POST'" or using the $_GET variable should populate your $category variable. Quote Link to comment https://forums.phpfreaks.com/topic/268184-help-with-phpmysql/#findComment-1376744 Share on other sites More sharing options...
cyberRobot Posted September 11, 2012 Share Posted September 11, 2012 One way is to merge both the PHP and JavaScript code. For example, here's a simplified code snippet from a Google Map I created earlier in the year. <?php $sql = "SELECT id, name, latLong FROM tableName ORDER BY name"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { ?> //ADD MAP MARKER FOR CURRENT ENTRY var marker<?php print $row['id']; ?> = new google.maps.Marker({ position: new google.maps.LatLng(<?php print $row['latLong']; ?>), map: map, title:"<?php print $row['name']; ?>" }); <?php } ?> If you prefer to add the markers with XML, the above code could be modified to meet those needs. Instead of loading the map after the submit button is clicked, you would load it when the PHP part is done using something like this: https://developers.google.com/maps/documentation/javascript/tutorial#asynch Quote Link to comment https://forums.phpfreaks.com/topic/268184-help-with-phpmysql/#findComment-1376998 Share on other sites More sharing options...
dkar Posted September 12, 2012 Author Share Posted September 12, 2012 Ok! Thanks. I will try to make it work by merging java and php together! Thanks a lot. D. Quote Link to comment https://forums.phpfreaks.com/topic/268184-help-with-phpmysql/#findComment-1377347 Share on other sites More sharing options...
cyberRobot Posted September 12, 2012 Share Posted September 12, 2012 Ok! Thanks. I will try to make it work by merging java and php together! Thanks a lot. D. For what it's worth, it's actually JavaScript. Java is a totally different programming language. I know picky, picky. Quote Link to comment https://forums.phpfreaks.com/topic/268184-help-with-phpmysql/#findComment-1377354 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.