This is the test page basic goal is as follows:
the user enter a bus route
get function modifies the mysql query inside php
query gets data from the dbs
php generates & sends back the xml
javascript on the html page creates markers and plots them onto the map.
// Opens a connection to a MySQL server
$connection = mysql_connect ($dbserver, $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 * FROM mydata WHERE routenumber = '$_GET[input]'";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<markers ';
echo 'stopname="' . parseToXML($row['stopname']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'routenumber="' . $row['routenumber'] . '" ';
echo 'routerun="' . $row['routerun'] . '" ';
echo '/>' . "\n";
}
// End XML file
echo '</busroute>';
?>
php on this page: http://ykothari.co.uk/test/xmlgen.php ( i tried but changing the '$_GET[input]' to a route number like 109 and the php query works and gives me a xml output, so i'm assuming i'm doing something wrong with php's get function)
If i put the file name itself into the form's action field, then I input a bus route it goes into the url and refreshs the page, but doesnt load anything.
If i put the php file's name into the form's action field, it redirects me to the xml generated by the php file, with the route number i inputed. i dont want it to redirect me, but rather process the xml and display it on the map.
Assuming you are sending some value through the url with a name "input", then this should work:
PHP Code:
$query = "SELECT * FROM mydata WHERE routenumber = '".$_GET["input"]."'"; // You could also echo $query for debugging purposes until you are satisfied you are getting the values you want. echo $query; // Delete this line after you are satisfied the value is there.
Assuming you are sending some value through the url with a name "input", then this should work:
PHP Code:
$query = "SELECT * FROM mydata WHERE routenumber = '".$_GET["input"]."'"; // You could also echo $query for debugging purposes until you are satisfied you are getting the values you want. echo $query; // Delete this line after you are satisfied the value is there.
I dont think that's the problem i tried them and both work fine. I think the problem is php script's inablilty to read the value generated by the text box.
If i put the following into the html body, the xml reply is empty.
Note: action field has the html page itself linked to it.
Changing, the ACTION field to the php script it accepts the value and generates the appropriate xml.
But what happens is my page gets redirected from the html to the xml (php script). I want the php script to send the xml to the html, and the javascript within the html to process it, while still staying at the html page.
static route html: Working gmaps with php generated xml
static route php: http://ykothari.co.uk/gmap-php/xmlgen.php
This is the example without the user entering any data, its just a static route done with a "select * from table" statement, but i want to make it dynamic, so when user enters say a different route the php script gets appropriate xml.
<html>
<head>
<title>Working gmaps with php generated xml</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
//<![CDATA[
// this variable will collect the html which will eventually be placed in the side_bar
var side_bar_html = "";
// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there
var gmarkers = [];
// global "map" variable
var map = null;
// A function to create the marker and set up the event window function
function createMarker(latlng, name, routenumber, stopname, routerun) {
var contentString = '<b>Bus Route: </b>'+ routenumber + '<br>' + '<b>Stop Name: </b>' + stopname + '<br>' + '<b>Route Run: </b>' + routerun;
var marker = new google.maps.Marker({
position: latlng,
map: map,
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map, marker);
});
// save the info we need to use later for the side_bar
gmarkers.push(marker);
// add a line to the side_bar html
side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + name + '<\/a><br>';
}
// This function picks up the click and opens the corresponding info window
function myclick(i) {
google.maps.event.trigger(gmarkers[i], "click");
}
function initialize() {
// create the map
var myOptions = {
zoom: 12,
center: new google.maps.LatLng(51.416552,-0.105057),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
downloadUrl('xmlgen1.php?input=<?php echo $input?>', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("markers");
for (var i = 0; i < markers.length; i++) {
var stopname = markers[i].getAttribute("stopname");
var routenumber = markers[i].getAttribute("routenumber");
var routerun = markers[i].getAttribute("routerun");
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var marker = createMarker(point,stopname,routenumber,stopname,routerun);
var poly = xml.documentElement.getElementsByTagName("markers");
var pts = [];
for (var a = 0; a < poly.length; a++) {
pts[a] = new google.maps.LatLng(parseFloat(poly[a].getAttribute("lat")),
parseFloat(poly[a].getAttribute("lng")));
}
var poly = new google.maps.Polyline({
path: pts,
strokeColor: "#4444ff",
strokeOpacity: 0.5,
strokeWeight: 5
});
poly.setMap(map);
}
// put the assembled side_bar_html contents into the side_bar div
document.getElementById("side_bar").innerHTML = side_bar_html;
});
}
var infowindow = new google.maps.InfoWindow();
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 style="margin:0px; padding:0px;" onload="initialize()">
<!-- you can use tables or divs for the overall layout -->
<Form Name ="trial" Method ="GET" ACTION ="xmlgen1.php">
<table border="1">
<tr>
<td colspan="2">
Type route number: <input name="input" type="text"/>
<input type="submit" value="Search!">
</td>
</tr>
<tr>
<td valign="top" style="width:200px; text-decoration: underline; color: #4444ff;">
<div id="side_bar" style="overflow:auto; height: 640px;"></div>
</td>
<td>
<div id="map_canvas" style="width: 800px; height: 640px"></div>
</td>
</tr>
</table>
</body>
</html>
Php
Code:
<?php
require("dbserverinfo.php");
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 ($dbserver, $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());
}
if(!empty($_GET['input'])){
$input = $_GET['input'];
}
// Select all the rows in the markers table
$query = "SELECT * FROM mydata WHERE routenumber = '$input'";
//echo $query;
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<?xml version="1.0"?>' . "\n";
echo '<busroute>' . "\n";
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<markers ';
echo 'stopname="' . parseToXML($row['stopname']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'routenumber="' . $row['routenumber'] . '" ';
echo 'routerun="' . $row['routerun'] . '" ';
echo '/>' . "\n";
}
// End XML file
echo '</busroute>';
//echo "\n";
//echo $query;
?>
Is the HTML and the PHP all on the same page?
And if they are, how are they arranged?
Displaying the HTML and PHP in different sections doesn't tell us much.
In fact it just tends to confuse us even more.
You need to show us what is real.
You need to explain things.
I can see the variables that you are using, but you aren't clear as to the logic that is being used.
If the HTML and PHP are in separate files, then you need to specify the file names.
If the HTML and PHP are in the same file, then show us the contents of that file as it is written.
You need to be very specific.
You are dealing with computers not human beings.
Computers can only do what they are told to do, nothing more and nothing less.
So we need to know exactly what you are telling the computer to do.
That is the source code of the file. They are both seperate files; the html page which has the inputbox/map and the php that generates the xml.
File names are named "gmap-php.htm"l for the html & "xmlgen1.php" for the php script.
Your using the BODY's onload event listener to initialize the GOOGLE map.
And if there is any value inputted in the form say like "109" then you want the
initialize() function to pickup the value from the form and use it in this command:
The problem is that the file "gmap-php.html" isn't interpreting any PHP commands.
And the reason it isn't is because it has a file extension "html".
If you would rename the file to "gmap-php.php" then it would be able to work.
Then just make two more modifications:
1. Change this line: