Current location: Hot Scripts Forums » General Community » Script Requests » refreshing dropdown list when selecting another drop down list


refreshing dropdown list when selecting another drop down list

Reply
  #1 (permalink)  
Old 08-13-08, 04:14 AM
theighost theighost is offline
Newbie Coder
 
Join Date: Jul 2008
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
refreshing dropdown list when selecting another drop down list

i am using code igniter in the website i'm working on and i need php combined with any language to do:

for example i have a dropdown list named states that takes its values from a data base, and i have another dropdown list named cities that has its values from a database also.

when i click on a state the city dropdown list should show the cities in that state only.

i read that i need ajax...which i dont know hot to use

any hints or scripts?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-13-08, 10:25 AM
phillyhotshots phillyhotshots is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Philadelphia
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
1. Create Dropdown Menu (state_dropdown.php)
Code:
<?     
     echo "<form name=sel>\n";
     echo "States : <font id=states><select>\n";
     echo "<option value='0'>============</option> \n" ;
     echo "</select></font>\n";
     
     echo "Cities : <font id=cities><select>\n";
     echo "<option value='0'>=== none ===</option> \n" ;
     echo "</select></font>\n";
?>

<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};

function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
 if (req.readyState==4) {
      if (req.status==200) {
           document.getElementById(src).innerHTML=req.responseText; //retuen value
      }
 }
};
req.open("GET", "state.php?data="+src+"&val="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}

window.onLoad=dochange('states', -1);         // value in first dropdown
</script>
2. Select States and Cities to Show in Dropdown (state.php)
Code:
<?
//set IE read from page only not read from cache
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");

header("content-type: application/x-javascript; charset=tis-620");

$data=$_GET['data'];
$val=$_GET['val'];

//set database
$dbhost = "localhost";
$dbuser = "";
$dbpass = "";
$dbname    = "test";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");

if ($data=='states') {  // first dropdown
  echo "<select name='states' onChange=\"dochange('cities', this.value)\">\n";
  echo "<option value='0'>==== choose state ====</option>\n";
  $result=mysql_db_query($dbname,"select `id`, `state` from states order by `state`");
  while(list($id, $name)=mysql_fetch_array($result)){
       echo "<option value=\"$id\" >$name</option> \n" ;
  }
} else if ($data=='cities') { // second dropdown
  echo "<select name='cities' >\n";
  echo "<option value='0'>====choose cities ====</option>\n";                   
  $result=mysql_db_query($dbname,"SELECT `id`, `city` FROM cities WHERE `state_id` = '$val' ORDER BY `city` ");
  while(list($id, $name)=mysql_fetch_array($result)){
       echo "<option value=\"$id\" >$name</option> \n" ;
  }
}
echo "</select>\n";
?>
Example:http://superkeng.freehyperspace.com/state_dropdown.php
Download Source:http://www.megashare.com/233052
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-14-08, 08:41 AM
theighost theighost is offline
Newbie Coder
 
Join Date: Jul 2008
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
much appreciated that helps
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-14-08, 09:30 AM
phillyhotshots phillyhotshots is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Philadelphia
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
No problem, hope it works for you!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


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
refreshing dropdown list when selecting another drop down list theighost PHP 1 08-13-08 11:10 AM
Selecting current hour in dropdown list toth ASP 7 01-26-06 04:24 AM
trouble with the display of results and drop down list.. SummerL PHP 0 07-11-05 11:38 AM


All times are GMT -5. The time now is 10:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.