ajax,提交刷新自身页面或iframe,等等
解决方案 »
- 我是新手,想学PHP
- apache2+php5.2+mysql5.0网站为什么登陆后又回到原始状态?登陆不上?(ampserv5.2.0调试正常)
- 出一道题
- 一个奇怪的问题
- 如何编写详细的网站设计说明书,现在数据库已经设计完毕!题目比较大请进来看一下
- 求读取https页面内容的办法!
- 怎样在两个页面之间传递参数?请给个详细介绍。100分相送!!
- 不通过smtp服务器如何发信?直接发到对方信箱如何实现?顶者有分
- 请教PHP与MYSQL 问题?
- 怎么读取sql某个表单把字段网格显示在页面上?
- 请教一下,php高级软件工程师,大概是什么水平?
- 怎么从mysql数据库中随机选出五条数据,每次取的结果不同
select的onchange事件发生时用xmlhttp访问远程数据库中的数据,得到的数据用JS装入另一个select中,也就是AJAX
js+dom 先去除另外一个select的option(s),再将所得结果加入option(s).
<?php
$functions=<<<funstr
function set_array(names,values){
this.names=names;
this.values=values;
}
function build_options(object_data,selected_index){
var optionstr="";
if(selected_index<=0){
selected_index=0;
}
for (i=0;i<object_data[selected_index].names.length;i++){
optionstr+="<option value='"+object_data[selected_index].names[i].values+"'"
+(selected_index==object_data[selected_index].names[i].values?" selected":"")+">"
+object_data[selected_index].names[i].names+"</option>";
}
document.write(optionstr);
}
function parent_change(parent_obj,target_obj,target_obj_data,selected_index){
if(!parent_obj.value) return;
for(k=0;k<target_obj_data.length;k++){
if (target_obj_data[k].values==parent_obj.value){
var son_obj=target_obj_data[k];
break;
}
}
if(!son_obj || !target_obj) return;
object_options = target_obj.options;
while(object_options.length>0){
object_options.remove(0);//remove all options
}
if(selected_index<0){
var new_options = document.createElement("OPTION");
new_options.text="select Pls";
new_options.value="";
object_options.add(new_options)
}
for(i=0;i<son_obj.names.length;i++){
var new_options = document.createElement("OPTION");
new_options.text=son_obj.names[i].names;
new_options.value=son_obj.names[i].values;
if(selected_index){
if(i==selected_index){
new_options.selected=true;
}
}
object_options.add(new_options);
}
}
funstr;
echo $functions."\n";include $_SERVER["DOCUMENT_ROOT"]."/data_config.php";
$amazon_tk->conn_db();
$country_sql="select * from tblcountry where ftype='2' order by fcountry_id";
$country_array=$amazon_tk->query($country_sql);
$rows_country=count($country_array);
$str_country_element="var country_element0=new Array(\n";
$str_state_array="var state_array=new Array(\n";
$str_state_element="";
for ($i=0;$i<$rows_country;$i++){
if (!$country_array[0]["fshort_name"]){
break;
}elseif($i==$rows_country-1){
$str_country_element=$str_country_element." new set_array(\"".$country_array[$i]["fshort_name"]."\",\"".$country_array[$i]["fshort_name"]."\")\n);\n";
$str_state_array=$str_state_array." new set_array(state_element".$country_array[$i]["fcountry_id"].",\"".$country_array[$i]["fshort_name"]."\")\n);\n";
//build state element
$str_state_element.="var state_element".$country_array[$i]["fcountry_id"]." = new Array (\n";
$state_sql="select * from tblstate where fcountry_id='".$country_array[$i]["fcountry_id"]."' order by fstate_name ";
$state_array=$amazon_tk->query($state_sql);
$rows_state=count($state_array);
for ($j=0;$j<$rows_state;$j++){
if ($j==$rows_state-1){
$str_state_element=$str_state_element." new set_array(\"".$state_array[$j]["fstate_name"]."\",\"".$state_array[$j]["fstate_name"]."\")\n);\n";
}else{
$str_state_element=$str_state_element." new set_array(\"".$state_array[$j]["fstate_name"]."\",\"".$state_array[$j]["fstate_name"]."\"),\n";
}
}
// build state end
}else{
$str_country_element=$str_country_element." new set_array(\"".$country_array[$i]["fshort_name"]."\",\"".$country_array[$i]["fshort_name"]."\"),\n";
$str_state_array=$str_state_array." new set_array(state_element".$country_array[$i]["fcountry_id"].",\"".$country_array[$i]["fshort_name"]."\"),\n";
//build state element
$str_state_element.="var state_element".$country_array[$i]["fcountry_id"]." = new Array (\n";
$state_sql="select * from tblstate where fcountry_id='".$country_array[$i]["fcountry_id"]."' order by fstate_name ";
$state_array=$amazon_tk->query($state_sql);
$rows_state=count($state_array);
for ($j=0;$j<$rows_state;$j++){
if ($j==$rows_state-1){
$str_state_element=$str_state_element." new set_array(\"".$state_array[$j]["fstate_name"]."\",\"".$state_array[$j]["fstate_name"]."\")\n);\n";
}else{
$str_state_element=$str_state_element." new set_array(\"".$state_array[$j]["fstate_name"]."\",\"".$state_array[$j]["fstate_name"]."\"),\n";
}
}
// build state end
}
}
//
$str_country_array="var country_array=new Array(\n new set_array(country_element0,\"0\")\n);\n";echo $str_country_element."\n";
echo $str_country_array."\n";
echo $str_state_element."\n";
echo $str_state_array;
?>