Can't get it to work, form,php, mysql (Solved )

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ahunter61
Forum Newbie
Posts: 2
Joined: Sat Jan 03, 2009 8:59 am

Can't get it to work, form,php, mysql (Solved )

Post by ahunter61 »

Can't get this to work , I am using this form to search for locations

<form name="location" method="post" action="/phpsqlsearch_genxml1.php" enctype="text/plain" id="Form1">
<input type="submit" id="Button1" name="Button1" value="Submit" style="position:absolute;left:164px;top:47px;width:96px;height:25px;font-family:Arial;font-size:13px;z-index:0">
<select name="location" size="1" id="Combobox1" style="position:absolute;left:31px;top:47px;width:96px;font-family:Courier New;font-size:16px;z-index:1" title="location">
<option>Maine</option>
<option>Florida</option>
</select>
</form>

and this PHP to process and return the xml needed for my map API
if I change $query = "SELECT * FROM markers WHERE location='$location'"; to $query = "SELECT * FROM markers WHERE location='Maine'";
it works fine but I need it to return the results from the form . any help would be greatly appreciated

Code: Select all

<?php  
 
require("databaseinfo.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 (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
 
 
$location = $_POST['location'];
 
$query = "SELECT * FROM markers WHERE location='$location'";
$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("name",$row['name']);
  $newnode->setAttribute("address", $row['address']);  
  $newnode->setAttribute("lat", $row['lat']);  
  $newnode->setAttribute("lng", $row['lng']);  
  $newnode->setAttribute("location", $row['location']); 
} 
 
echo $dom->saveXML();
 
?>
Last edited by ahunter61 on Sat Jan 03, 2009 12:21 pm, edited 1 time in total.
User avatar
thisismyurl
Forum Newbie
Posts: 15
Joined: Wed Dec 03, 2008 8:00 am

Re: Can't get it to work, form,php, mysql

Post by thisismyurl »

In the line:

Code: Select all

<select name="location" size="1" id="Combobox1" style="position:absolute;left:31px;top:47px;width:96px;font-family:Courier New;font-size:16px;z-index:1" title="location">
You are naming the form element "location" but still ID'ing it as "Combobox1". If you change Combobox1 to location, it should work for you.

Also, some servers tend to be a little picky about how you call variables. In your code you are searching for $location, which on some servers will return the results stored in $_REQUEST['location'] but on others it won't. Just to be on the safe site, I'd put the line $location = $_REQUEST['location']; before your SQL.

Let me know if that helps at all. Chris
ahunter61
Forum Newbie
Posts: 2
Joined: Sat Jan 03, 2009 8:59 am

Re: Can't get it to work, form,php, mysql

Post by ahunter61 »

Thanks, I changed all the ids to states instead of location to avoid confusion but it still won't work, It seems like my form information is not reaching the $query = "SELECT * FROM markers WHERE states='$states'"; and searching for florida or maine it just returns a blank xml page,

Thxs Pete
Post Reply