Current location: Hot Scripts Forums » Programming Languages » PHP » Php GET function problem


Php GET function problem

Reply
  #1 (permalink)  
Old 03-17-11, 06:47 PM
yazz_1988 yazz_1988 is offline
Newbie Coder
 
Join Date: Mar 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Php GET function problem

I'm trying to get a user input form working but i'm facing some problems.

Working gmaps with php generated xml

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.


PHP Code:

<?php

require("dbserverinfo.php");

function 
parseToXML($htmlStr

$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",''',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$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());
}
 
// 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());
 }

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>';
?>
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.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 03-17-11, 08:46 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
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. 
__________________
Jerry Broughton
Reply With Quote
  #3 (permalink)  
Old 03-17-11, 09:41 PM
yazz_1988 yazz_1988 is offline
Newbie Coder
 
Join Date: Mar 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
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 tried it, no luck.

I checked the firebug to find that there is no xml being generated by the script.
http://i.imgur.com/NCr6S.png

the value is being sent by this line.
Code:
 
		downloadUrl('xmlgen1.php?input=<?php echo $input?>', function(data) {
the input box is indeed called input.
Code:
 
	<Form Name ="trial" Method ="GET" ACTION = "gmap-php.html">		
    <table border="1">
	
		<tr>
		<td colspan="2">
		Type route number: <input name="input" type="text"/>
		</td>
		</tr>
Tried with echo query too at the end of php but same thing no input is being detected. This was the response in firebug.
Code:
<?xml version="1.0"?>
<busroute>
</busroute>
SELECT * FROM mydata WHERE routenumber = '<?php echo $input?>'

Last edited by yazz_1988; 03-17-11 at 09:42 PM. Reason: Addtion
Reply With Quote
  #4 (permalink)  
Old 03-18-11, 04:02 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I see a potential problem here:
PHP Code:

downloadUrl('xmlgen1.php?input=<?php echo $input?>', function(data) {
Shouldn't it be like this?
PHP Code:

downloadUrl('xmlgen1.php?input=<?php echo $_GET["input"]; ?>', function(data) {
__________________
Jerry Broughton
Reply With Quote
  #5 (permalink)  
Old 03-18-11, 11:45 AM
yazz_1988 yazz_1988 is offline
Newbie Coder
 
Join Date: Mar 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
I see a potential problem here:
PHP Code:

downloadUrl('xmlgen1.php?input=<?php echo $input?>', function(data) {
Shouldn't it be like this?
PHP Code:

downloadUrl('xmlgen1.php?input=<?php echo $_GET["input"]; ?>', function(data) {
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.
Code:
	<Form Name ="trial" Method ="GET" ACTION = "gmap-php.html">		
    <table border="1">
	
		<tr>
		<td colspan="2">
		Type route number: <input name="input" type="text"/>
		</td>
		</tr>
Note: action field has the php page linked to it.
Code:
	<Form Name ="trial" Method ="GET" ACTION = "xmlgen1.php">		
    <table border="1">
	
		<tr>
		<td colspan="2">
		Type route number: <input name="input" type="text"/>
		</td>
		</tr>
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.

Hope this isnt too confusing.
Reply With Quote
  #6 (permalink)  
Old 03-19-11, 10:03 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You need to show us your code so we can see all that is happening.
But I believe that you will need to use AJAX to make this work.
__________________
Jerry Broughton
Reply With Quote
  #7 (permalink)  
Old 03-20-11, 08:19 PM
yazz_1988 yazz_1988 is offline
Newbie Coder
 
Join Date: Mar 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Html
Code:
<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('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",''',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$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;
?>
Reply With Quote
  #8 (permalink)  
Old 03-21-11, 06:54 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
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.
__________________
Jerry Broughton
Reply With Quote
  #9 (permalink)  
Old 03-22-11, 12:20 PM
yazz_1988 yazz_1988 is offline
Newbie Coder
 
Join Date: Mar 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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.

They can be found here ->
Working gmaps with php generated xml
http://ykothari.co.uk/test/xmlgen1.php

Code:
 require("dbserverinfo.php");
dbserverinfo.php just has the databasename/username/password/database server name

The sql dump of the database is on this link
http://ykothari.co.uk/test/mydata.sql
Reply With Quote
  #10 (permalink)  
Old 03-22-11, 10:09 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Here's the problem.

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:
PHP Code:

downloadUrl('xmlgen1.php?input=<?php echo $input?>', function(data) {
.

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:
PHP Code:

downloadUrl('xmlgen1.php?input=<?php echo $input?>', function(data) {
To:
PHP Code:

downloadUrl('xmlgen1.php?input=<?php echo $_POST["input"]; ?>', function(data) {
And

2. Change this line:
HTML Code:
<Form Name ="trial" Method ="GET" ACTION ="xmlgen1.php">
To:
HTML Code:
<form name="trial" method="POST" action="#">
__________________
Jerry Broughton

Last edited by job0107; 03-22-11 at 10:15 PM.
Reply With Quote
Reply

Bookmarks

Tags
javascript, php, xml


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Javascript in a php page binarybird PHP 1 06-21-10 05:57 AM
PHP Search Feature Problem jilawatan PHP 2 08-13-09 08:21 AM
Problem with CSS and PHP working together:( cssgopher PHP 7 03-24-08 03:31 AM
php function installation from php.net?? DangerZone PHP 5 11-09-07 10:28 PM
Urgent problem with PHP domxml_open_mem(), php extension php_domxml.dll. Need help! Oskare100 Web Servers 3 01-03-07 05:08 AM


All times are GMT -5. The time now is 08:47 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.