Page 1 of 1

PHP Dropdown

Posted: Fri Apr 08, 2011 1:10 am
by aliusman
I am creating a php page including 3 dropdowns. i need code for it

when i select combo box 1 it will filters the value of combo box 2. and when i select the value in combo box 2 it filters the value of combo box 3.

sample link

http://www.umnet.com/Controls/res_Browse.aspx

Re: PHP Dropdown

Posted: Fri Apr 08, 2011 12:52 pm
by social_experiment
Can you paste any code that you have created?

Re: PHP Dropdown

Posted: Sun Apr 10, 2011 10:52 pm
by aliusman
index.php

Code: Select all

<html>
<head>
<title>dropdown code</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function getXMLHTTP() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
	}
	
	
	
	function getCity(strURL) {		
		
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('citydiv').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}
				
	}
</script>




</head>

<body>

<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150">Country</td>
    <td  width="150"><select name="country" onChange="getCity('findcity.php?country='+this.value)">
	<option value="">Select Country</option>
	<option value="1">USA</option>
	<option value="2">Canada</option>
        </select></td>
  </tr>
  <tr style="">
    <td>City</td>
    <td ><div id="citydiv"><select name="city">
	<option>Select City</option>
        </select></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

findcity.php

Code: Select all

<? $country=$_REQUEST['country'];
$link = mysql_connect('localhost', 'root', ''); 

if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="select city from city where countryid=$country";
$result=mysql_query($query);

?>
<select name="city">
<option>Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>
db_ajax

# phpMyAdmin MySQL-Dump
# version 2.3.2
# http://www.phpmyadmin.net/ (download page)
#
# Host: localhost
# Generation Time: Aug 14, 2006 at 09:14 PM
# Server version: 4.00.00
# PHP Version: 4.2.3
# Database : `db_ajax`
# --------------------------------------------------------

#
# Table structure for table `city`
#

CREATE TABLE city (
id tinyint(4) NOT NULL auto_increment,
city varchar(50) default NULL,
countryid tinyint(4) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;

#
# Dumping data for table `city`
#

INSERT INTO city VALUES (1, 'Los Angales', 1);
INSERT INTO city VALUES (2, 'New York', 1);
INSERT INTO city VALUES (3, 'Toranto', 2);
INSERT INTO city VALUES (4, 'Vancovour', 2);

# --------------------------------------------------------

#
# Table structure for table `country`
#

CREATE TABLE country (
id tinyint(4) NOT NULL auto_increment,
country varchar(20) NOT NULL default '',
PRIMARY KEY (id)
) TYPE=MyISAM;

#
# Dumping data for table `country`
#

INSERT INTO country VALUES (1, 'USA');
INSERT INTO country VALUES (2, 'Canada');

Re: PHP Dropdown

Posted: Sun Apr 10, 2011 10:53 pm
by aliusman
its urgent. waiting 4 ur reply

Re: PHP Dropdown

Posted: Mon Apr 11, 2011 12:57 pm
by califdon
Here's a reference that may be of help: http://devzone.skillfusion.com/ajaxDropdown.php

Re: PHP Dropdown

Posted: Wed Apr 13, 2011 4:10 am
by aliusman
its not working

Re: PHP Dropdown

Posted: Wed Apr 13, 2011 11:46 am
by social_experiment

Code: Select all

<div id="citydiv">
<!-- try this -->
<select name="city" id="citydiv">
<option>Select City</option>
</select>
Give the <select> element the id instead of the div.