ajax,提交刷新自身页面或iframe,等等

解决方案 »

  1.   


    select的onchange事件发生时用xmlhttp访问远程数据库中的数据,得到的数据用JS装入另一个select中,也就是AJAX
      

  2.   

    xmlhttp条件读取数据库查询结果,text/json或xml形式。
    js+dom 先去除另外一个select的option(s),再将所得结果加入option(s).
      

  3.   

    可以帮忙写个例子吗?xmlhttp和js我都不懂了,谢谢了!
      

  4.   

    不用ajax也可以。不过,这要你使用更加多的、庞大的代码了主要实现原理:1、使用php程序写一js脚本文件。脚本文件内容就是你所有涉及到的前后分类信息了。2、调用这个脚本,然后根据第一个select的change,在后面写入不同的option内容。
      

  5.   

    谢谢li1229363() 按你的思路写了个:
    <?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;
    ?>