Page 1 of 1

Dynamic List Boxes from a database.

Posted: Mon Oct 25, 2004 7:22 pm
by Kingo
Hello I'm trying to implement dynamic list boxes from the database. i used the following code whihc I got from google. I'm not a very good programmer , so was not able to understand the code. I would really appreciate if any one would give me a simpler code to implent this.

Code: Select all

<?php 
echo "<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> ";
echo "<title>List</title></head>   <body> ";
echo "Hello";

require_once('Connections/mysql_conn.php');


echo "<FORM name=f1  action="$_SERVER[PHP_SELF]" method=post>"; 

$result = mysql_query("SELECT tblCountry.CountryName,tblCity.Countryid,tblCity.CityName,tblCity.id FROM tblCity,tblCountry WHERE tblCity.Countryid=tblCountry.id"); 

echo "<CENTER><BR><B>Dynamic List Boxes Demo (in PHP)</B><BR>"; 
echo "<TABLE font style='font-family: verdana; font-size: 12; font-weight:700' border=1>"; 

// write the country's listbox... 
   
echo "<TR><TD valign="center">Country</TD><TD><SELECT NAME="country" SIZE="1"  ONCHANGE="countryselected(this);" >\n"; 
$sJavaScript = "function countryselected(elem){\n for (var i = document.f1.city.options.length; i >= 0; i--){ \n document.f1.city.options[i] = null;\n"; 
$sLastCountry=""; 
while ( $row = mysql_fetch_array($result) ) 
  {    
    // is this a new country? 

    If ($sLastCountry!=$row["CountryName"]){ 
     
      // if yes, add the entry to the country's listbox 

      $sLastCountry = $row["CountryName"]; 
      echo "\n<OPTION VALUE='".$row["Countryid"]."'>".$sLastCountry."</OPTION>"; 
       
    // and add a new section to the javascript... 

      $sJavaScript = $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value==".$row["Countryid"]."){\n"; 
     } 
    // and add a new city line to the javascript 

    $sJavaScript = $sJavaScript."document.f1.city.options[document.f1.city.options.length] = new Option('".$row["CityName"]."','".$row["id"]."');\n"; 

  } 
// finish the country's listbox 

  echo "</SELECT></TD>"; 

  // create the city listbox for no selection 

  echo "\n<TD valign="center">City</TD><TD><SELECT NAME="city" SIZE=10>"; 
  echo "<OPTION>[no city selected]</OPTION>"; 
  echo "</SELECT></TD></TR>"; 
  echo "<TR><TD><font style='font-size=10'></TD><TD></TD><TD></TD><TD><INPUT TYPE=SUBMIT NAME='submitcity' VALUE='SUBMIT'></TD></TR>"; 

echo "</TABLE>"; 
// finish the javascript and write out 

  $sJavaScript = $sJavaScript."\n}\n}\n"; 
  echo "\n<SCRIPT LANGUAGE="JavaScript">"; 
  echo "\n".$sJavaScript."\n</SCRIPT>\n"; 

//close the form 

echo "</FORM></center>"; 


//code to test the submit button 
//normally people would save the index in another table 
//this example only display the indexe
/*
if ("SUBMIT" == $submitcity) 
{ 

echo "<center>Your Selected Country index= ".$country."<BR>";   
echo "Your Selected City index= ".$city."<BR></center>";    

}    
*/
echo "</body></html>";
?>

Posted: Tue Oct 26, 2004 7:36 am
by phpScott
what are you going to want to use the code for?