donglan8870 2013-11-14 20:56
浏览 25
已采纳

3 Ajax和PHP依赖下拉

Good day ,

I have 3 PHP pages ('test1.php' , 'res.php' , 'desc.php') ,So I want to make 3 dependent drop down. all drop down dependent to previous drop down. So when I select an item from first drop down , all data fetch from database by 'res.php' and add to second drop down as item . But when I select second drop down , page will refresh and back to first step and all drop down item selected gone .

test1.php

<script>

function load_sub_cat(str){     

      var xmlhttp;
      xmlhttp=new XMLHttpRequest();
      xmlhttp.open("get","res.php?q="+str,false);

      xmlhttp.send();   

      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;     

            }
  </script>

        <select name="users" onChange="load_sub_cat(this.value)">
        <option value="0">Select a restaurant</option>
   <?php

         $db->setQuery("SELECT id, name FROM yh5lw_rest_dropdown where active=1");
         $results = $db->loadObjectList();

         foreach ($results as $result) {

         echo "<option value=".$result->id.">". $result->name."</option>";

        }

  ?>

   </select>
   <div id="txtHint"></div>

res.php

 <script>
 function load_sub_cat(str){


     var xmlhttp1;
     xmlhttp1=new XMLHttpRequest();
     xmlhttp1.open("get","desc.php?desc="+str,false);
     xmlhttp1.send(); 
     document.getElementById("txt23").innerHTML=xmlhttp1.responseText;

 }
</script>

<select name="users1" onChange="load_sub_cat(this.value)">
    <option value="">Select a main course</option>
    <?php
    $q = intval($_GET['q']);

    $db->setQuery("SELECT price,id, nameEN , nameFA FROM yh5lw_food_dropdown where active=1 AND res_id ='".$q."'");
    $results = $db->loadObjectList();

    foreach ($results as $result) {
        echo "<option value=".$result->id.">". $result->nameEN." &nbsp; (".$result->price"</option>";
        }
        $count++ ;
    }
    ?>
    </select> 
    <div id="txt23"></div>

desc.php

<?php echo $_GET['desc'] ?>
  • 写回答

1条回答 默认 最新

  • dpl74687 2013-11-14 21:23
    关注

    Although you can write your own code for this, you don't need to reinvent the wheel for this functionality - try the jQuery Cascading Dropdown Plugin, it's free and does precisely what you want to do. It's easy to setup, it supports AJAX, and and you'll only need to use your server-side code for the AJAX calls.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题