PHP Dropdown

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aliusman
Forum Newbie
Posts: 4
Joined: Fri Apr 08, 2011 12:58 am

PHP Dropdown

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Dropdown

Post by social_experiment »

Can you paste any code that you have created?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
aliusman
Forum Newbie
Posts: 4
Joined: Fri Apr 08, 2011 12:58 am

Re: PHP Dropdown

Post 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');
aliusman
Forum Newbie
Posts: 4
Joined: Fri Apr 08, 2011 12:58 am

Re: PHP Dropdown

Post by aliusman »

its urgent. waiting 4 ur reply
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP Dropdown

Post by califdon »

Here's a reference that may be of help: http://devzone.skillfusion.com/ajaxDropdown.php
aliusman
Forum Newbie
Posts: 4
Joined: Fri Apr 08, 2011 12:58 am

Re: PHP Dropdown

Post by aliusman »

its not working
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Dropdown

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply