Hi

I am new to PHP and looking to create a search php page, the search needs to be by Country, State and city. I have tried several methods over the last three weeks and none work, im slamming my head against my desk!

Basicly I have three dropdown boxes on the top of my php page that i start off with, dropdown 1 is Country - data is pulled from mysql table tbl_country,
Here comes teh problems - When Country is selected i want the States from the states table to be filled in in the dropdown (like with the onchange event) when the states are seleted the the city needs to be populated after teh city has been chosen the search can comence to populate the page and show the results for the selected

Is there any (WORKING) tutorials, videos or source code that can just show me how this is done

Mike :$

Recommended Answers

All 7 Replies

http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php

I downloaded the zip file, put under my xampp folder did the mysql section in db for tables, made changes to config ran it under localhost, it shows the boxes and first box has data, when selecting teh first one nothing happens ... seems like somethings wrong, i edited the DB information and it does nothing no errors no change just the boxes and button

Does Ajax run with Xampp, i looked and everyone says the could be issues, what alternative do i have

tx

Just deleted 194 files used in trying to create this #@!$! thing and still no luck.

Running Xampp on Windows 7 Ultimate, Dreamweaver MX, Mysql.

I picked up something strange and that might be the cause of nothing working. see image.

[IMG]http://www.branding4you.co.za/wtf1.jpg[/IMG]

inside the file exactly where the code starts displaying on the page (thats not suposed to) is the following code:

// Declare a variable for Assigning the Current Form Name
	 var $CurrectForm;
	 
	     
	function JSDropDown($dbConfig){
     //Purpose: Constructer to Handle Database And Value initialize 
              $this->db_con=mysql_connect($dbConfig['server'],$dbConfig['username'],$dbConfig['password']) or die(mysql_error());
		      mysql_select_db($dbConfig['database'],$this->db_con) or die(mysql_error($this->db_con));	  
			  // Declare the variables as Array
   	          $this->regionArray=array();   
	          $this->cityArray = array();			  
	}

so the error occurs at the ">" if im correct?
i have never seen the use of "$this->" ever
the full code of the prject is below.

jsDropdown.php

<?php 
// Script That is used to create Object and Perform the Function Call 
/* database configuration */ 
// Please use your database configurations 
$dbConfig['type']="mysql"; 
$dbConfig['server']="localhost"; 
$dbConfig['username']="root"; 
$dbConfig['password']=""; 
$dbConfig['database']="travelinit"; 
// Include the class 
include_once("JSDropDown.class.php"); 

//Declare the Form Name 
$frmObjName="JSddForm"; 
echo "<form name=\"$frmObjName\" method=\"post\">"; 

$JSdd = new JSDropDown($dbConfig); 
$JSdd->DataFetch(); // Call the Function which will fetch the data. 
$JSdd->createfrmObject($frmObjName); // Pass the Current Form name as the parameter 
 
$JSdd->close_JSDropDown_database(); 
echo "</form>"; 
?>

JSDropDown.class.php

<?

class JSDropDown{

	 //Member varible Declaration
	 var $db_con;
	 var $rs_row;
 	 var $ERR_MSG; 
	 var $regionArrayResult;
	 
	 // Declare Array Variable;
	 var $cityArray;
	 var $regionArray;

     // Declare a variable for Assigning the Current Form Name
	 var $CurrectForm;
	 
	     
	function JSDropDown($dbConfig){
     //Purpose: Constructer to Handle Database And Value initialize 
              $this->db_con=mysql_connect($dbConfig['server'],$dbConfig['username'],$dbConfig['password']) or die(mysql_error());
		      mysql_select_db($dbConfig['database'],$this->db_con) or die(mysql_error($this->db_con));	  
			  // Declare the variables as Array
   	          $this->regionArray=array();   
	          $this->cityArray = array();			  
	}
	
	function close_JSDropDown_database(){
	//Purpose: Close the Database Connectivity
	    mysql_close($this->db_con);
	}
	
	
	function DataFetch(){ 
	//Purpose: Fetch the Data From 2 Different Table and store it in array
	
	// Suggestions: If you have changed the Table Names Please Change it in the sql;
	    $citySql="select * from citymaster";   
		$cityResult=mysql_query($citySql);
		$IDx=0;$IDy=0;$IDz=0;
		$this->regionArrayResult=array();
		while ($cityRow = mysql_fetch_array($cityResult,MYSQL_BOTH)){
			for($i=0;$i<mysql_num_fields($cityResult);$i++){
					$this->cityArray[$IDx][$i] = $cityRow[$i];
			}	
			$regionSql="select * from region WHERE city_id='".$cityRow["city_id"]."'";		   
			$regionResult=mysql_query($regionSql);
			$IDz=mysql_num_rows($regionResult);
			while($regionRow = mysql_fetch_array($regionResult)){
			        for($i=0;$i<mysql_num_fields($regionResult);$i++){
					    $this->regionArray[$IDy][$i] = $regionRow[$i];
					}
					$this->regionArray[$IDy][$i]=$cityRow[1]; 		
					$IDy+=1;         
			}// Region While Loop Ends here;
		   $IDx+=1; 
		} // City Region Loop End 
	}// DataFetch Function Ends here
	
	function createfrmObject($frmObjName){ 
	//Purpose: Create the Javascript Dynamically
	    $this->CurrectForm= $frmObjName;
	    $DocElement = "document.".$this->CurrectForm.".region";
      echo "<script type=\"text/javascript\" language=\"javascript\">";
		  echo "<!--\n";
      echo "\rfunction changeRegion(obj){\r";
      for($i=0;$i<count($this->cityArray);$i++){
         echo "\rif(obj.value == '".$this->cityArray[$i][0]."'){\r";
         echo "\r\t/* Value for the Region Drop Down */\r";
         echo "\tdocument.$this->CurrectForm.region.options.length = 0;\r";
         echo "\t$DocElement.options[$DocElement.options.length] = new Option('--Select--','--Select--');\r";
         for($j=0;$j<Count($this->regionArray);$j++){
             if ( $this->cityArray[$i][0] == $this->regionArray[$j][1]){
                echo "\t$DocElement.options[$DocElement.options.length] = new Option('".$this->regionArray[$j][2]."','".$this->regionArray[$j][0]."');\r";
             }
         }
         echo "\tdocument.$this->CurrectForm.region.disabled = false;\n\r";
         echo "\treturn true;\r";
         echo "}\r";
                      
      }
   		echo "}\r";
	 	  echo "-->\r";
	    echo "</script>";	
	    $this->CreateSelect();
   }
    
   function CreateSelect(){
        echo "<select name=\"city\" size=\"1\" onChange='changeRegion(this)'>\r";
		    echo "<option value='--Select--'>--Select--</option>\r";
   		  for($i=0;$i<count($this->cityArray);$i++){
		         echo "<option value='".$this->cityArray[$i][0]."'>".$this->cityArray[$i][1]."</option>\r";
  	    }
		    echo "</select> &nbsp; Area&nbsp;";
        echo "<select name=\"region\" disabled>\r";
        echo "<option value='--Select--'>--Select--</option>\r";		 
	      echo "</select>\r";
   }
	
} // Class JSDropDown Ends Here
?>

sql_file.sql

# phpMyAdmin MySQL-Dump
# version 2.5.0
# http://www.phpmyadmin.net/ (download page)
#
# Host: localhost
# Server version: 4.0.12
# PHP Version: 4.3.3
# --------------------------------------------------------

#
# Table structure for table `citymaster`
#
# Creation: Jan 07, 2005 at 12:49 AM
# Last update: Jan 07, 2005 at 12:49 AM
#

CREATE TABLE `citymaster` (
  `city_id` int(11) NOT NULL auto_increment,
  `cityname` varchar(100) default NULL,
  PRIMARY KEY  (`city_id`),
  UNIQUE KEY `city_id` (`city_id`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;

#
# Dumping data for table `citymaster`
#

INSERT INTO `citymaster` VALUES (1, 'Raleigh');
INSERT INTO `citymaster` VALUES (2, 'Durham');
INSERT INTO `citymaster` VALUES (3, 'Hillsborough');
INSERT INTO `citymaster` VALUES (4, 'Chapell Hill');
INSERT INTO `citymaster` VALUES (5, 'Cary');


# Table structure for table `region`
#
# Creation: Jan 07, 2005 at 12:49 AM
# Last update: Jan 07, 2005 at 12:49 AM
#

CREATE TABLE `region` (
  `rid` int(11) NOT NULL auto_increment,
  `city_id` int(11) NOT NULL default '0',
  `region_name` varchar(250) NOT NULL default '',
  PRIMARY KEY  (`rid`)
) TYPE=MyISAM AUTO_INCREMENT=6 ;

#
# Dumping data for table `region`
#

INSERT INTO `region` VALUES (1, 4, 'NC');
INSERT INTO `region` VALUES (2, 1, 'North Raleigh');
INSERT INTO `region` VALUES (3, 1, 'Northwest Raleigh');
INSERT INTO `region` VALUES (4, 1, 'Downtown Raleigh');
INSERT INTO `region` VALUES (5, 1, 'SouthEast Raleigh');

thats what i see too ... although all other php stuff works ... i use code like

index.php

<?php

require("connect.php");

//EXTRACT DATA
$extract = mysql_query("SELECT * FROM People");
echo mysql_num_rows($extract);

$numrows = mysql_num_rows($extract);

while ($row = mysql_fetch_assoc($extract))
{

$id = $row['id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$dob = $row['dob'];
$gender = $row['gender'];

echo "$firstname <br>";
}
?>

and ...

connect.php

<?php

error_reporting (0);
$connect = mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("boxit_db") or die (mysql_error());
echo "Connected to Database <p>";

?>

so why does this not work ... is it the charset ?

I had been working with this for a while and i have created a snippet for myself. Here it is. You can copy the complete page and you can work it out for further usage. This applies to anybody who lands this page.

http://vikku.info/programming/geodata/geonames-get-country-state-city-hierarchy.htm

Just view source that page and download all the includes in script tag. Thats it. I am further expanding my description so in due course of time there will be more information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.