大体思路是:先用PHP动态生成Javascript代码,再用生成的javascript执行选择功能.
1.values.php
<?php
$servername="localhost";
$dbuser="root";
$dbpassword="";
$dbname="taskmanage";
?>
2.Tdb_query.php....
mysql数据库执行类:
<?php
include ("values.php");
class Tdb_query
{
var $query_number=0;
var $linkID;
var $sql;
var $result;
function connect()
{
global $servername,$dbuser,$dbpassword,$dbname;
if($this->linkID = @mysql_connect($servername,$dbuser,$dbpassword))
{
mysql_select_db($dbname,$this->linkID);
return true;
}
else
{
return false;
}
}
function pconnect()
{
global $servername,$dbuser,$dbpassword,$dbname;
if($this->linkID = @mysql_pconnect($servername,$dbuser,$dbpassword))
{
mysql_select_db($dbname,$this->linkID);
return true;
}
else
{
return false;
}
}
function query($sql)
{
$this->query_number++;
$this->sql = $sql;
$this->result = mysql_query($sql,$this->linkID);
return true;
}
function fetcharray()
{
return mysql_fetch_array($this->result);
}
function close()
{
mysql_close($this->linkID);
}
function queryNumbers()
{
return $this->query_number;
}
function numrows()
{
return mysql_num_rows($this->result);
}
function affectedrows()
{
return mysql_affected_rows($this->linkID);
}
function insertid()
{
return mysql_insert_id($this->linkID);
}
function result($row,$field)
{
return mysql_result($this->result,$row,$field);
}
function source()
{
return $this->sql;
}
function numFields()
{
return mysql_num_fields($this->result);
}
function errno(){
return mysql_errno($this->linkID);
}
function fetchrows(){
return mysql_fetch_row($this->result);
}
}
?>
<SCRIPT language=javascript>
<?php
include("Tdb_query.php");
include("function.php");
$mydb=new Tdb_query;
if (!$mydb->connect()) {
dealwitherror("dberror");
exit();
}
$db["depart_sql"]="select * from tvalue where type='department'";
$mydb->query($db["depart_sql"]);
while ($s=$mydb->fetcharray()) {
$db["depart_contents"][]=$s["content"];
$db["depart_id"][]=$s["id"];
}
if ($num=$mydb->numrows()) {
?>
cityareaname=new Array(<?php echo $num;?>);
cityareacode=new Array(<?php echo $num;?>);
function first(preP,preC,formname,selectP,selectC)
{
a=0;
<?php
global $num;
for ($i=1;$i<=$num;$i++)
{ $dep_res["content"]=$db["depart_contents"][$i-1];
$db["zhuanye_sql"]="select * from tvalue where con_par_id=".$db["depart_id"][$i-1];
$mydb->query($db["zhuanye_sql"]);
if(!($mydb->numrows())){
// echo "请先为".$dep_res["content"]."系分配专业~_~";
// exit;
}else
{
unset($zhuanye_contents);
while ($r=$mydb->fetcharray()) {
$zhuanye_contents[]=$r["content"];
}
$p=$i;
if ($i<9){$j='0'.$p;echo "if(selectP=='$j')";$f=$j;}
else {echo "if(selectP=='$p')";$f=$p;}
echo "{ a=$i;tempoption=new Option(";
echo "'{$dep_res['content']}','$f',false,true);}";
echo "else/n{/n";
echo "tempoption=new Option("."'{$dep_res['content']}','$f');}/n";
echo "eval('document.'+formname+'.'+preP+'.options[$i]=tempoption;');";
$c=$i-1;
echo "cityareacode[$c]=new Array(";
for ($g=0;$g<$mydb->numrows()-1;$g++)
{ $m=$g+1;
if($g<9){$m='0'.$m;}
$xi_id=$f.$m;
echo "'$xi_id',";
}
$m=$g+1;
if($g<9){$m='0'.$m;}
$xi_id=$f.$m;
echo "'$xi_id');";
echo "cityareaname[$c]=new Array(";
for ($g=0;$g<$mydb->numrows()-1;$g++)
{ $temp=$zhuanye_contents[$g];
echo "'$temp',";
}
$temp=$zhuanye_contents[$g];
echo "'$temp');";
}
}
?>
cityid=selectP;
if (cityid!='0')
{
b=0;for (i=0;i<cityareaname[cityid-1].length;i++)
{
if (selectC==cityareacode[cityid-1][i])
{b=i+1;tempoption=new Option(cityareaname[cityid-1][i],cityareacode[cityid-1][i],false,true);}
else
tempoption=new Option(cityareaname[cityid-1][i],cityareacode[cityid-1][i]);
eval('document.'+formname+'.'+preC+'.options[i+1]=tempoption;');
}
eval('document.'+formname+'.'+preC+'.options[b].selected=true;');
}
}
function selectcityarea(preP,preC,formname)
{
cityid=eval('document.'+formname+'.'+preP+'.selectedIndex;');
j=eval('document.'+formname+'.'+preC+'.length;');
for (i=1;i<j;i++)
{eval('document.'+formname+'.'+preC+'.options[j-i]=null;')}
if (cityid!="0")
{
for (i=0;i<cityareaname[cityid-1].length;i++)
{
tempoption=new Option(cityareaname[cityid-1][i],cityareacode[cityid-1][i]);
eval('document.'+formname+'.'+preC+'.options[i+1]=tempoption;');
}
}
}
</SCRIPT>
<SCRIPT language=javascript>
first("selectp","selectc","form1",0,0);
</SCRIPT>
<FORM
action=file:///C|/Program%20Files/Macromedia/Dreamweaver%204/Lessons/Lesson%20Files/info_search.asp?martid=
method=post name=form1><IMG height=6 src="" width=1><BR>
<TABLE border=0 cellPadding=1 cellSpacing=0 width=300
!bgcolor="#FFFDE6"><TBODY>
<TR>
<TD align=right class=black_con9>选择系:</TD>
<TD class=black_con9><SELECT name=selectp
οnchange="selectcityarea('selectp','selectc','form1');"> <OPTION
selected value=0>请选择系..</OPTION></SELECT> </TD></TR>
<TR>
<TD align=right class=black_con9>选择专业:</TD>
<TD class=black_con9><SELECT name=selectc> <OPTION selected
value=0>请选择专业..</OPTION></SELECT> </TD></TR></TBODY></TABLE></FORM>
<?php
}
?>