douzhanyan5015 2010-11-14 19:53
浏览 75

如何将sql查询结果传递给javascript函数?

hello I wanted to know whats the best way to send sql results to a javascript function. I have two dropdown lists and i want to populate the second one according to the first one. both dropdown lists arre populated from a mysql database. fillcourses is the javascript function but i dont know how to pass the result array to it.

$db = new Db();
    $db->query('SELECT idDepartment,name FROM department');
    <select name="department" onchange=" fill_courses (
        SELECT courseNumber,courseId FROM courses 
        WHERE Department_idDepartment = document.addCourse.department.selectedIndex.value); ">';
        while ($row=$db->nextRow())
        {
            echo'<option value="' . $row['idDepartment'] . '">' . $row['name'] . '</option>';
        }
        echo'</select>
        <select name="courses">
        </select>
  • 写回答

3条回答 默认 最新

  • dtdd25012 2010-11-14 20:04
    关注

    You may want to consider between two main options:

    1. Either generate the JavaScript code on the server-side in PHP just before the response is sent to the browser. In this case, you will have all the data held within the browser. This will be very fast, but it is only feasible if you do not have a large amount of data to populate your drop-downs with.

    2. You could also use Ajax, so that when you change the value of your first drop-down your browser will initiate an asynchronous request to your server, which in turn will respond with the data to populate your second drop-down. You may want to use a JavaScript library such as jQuery to help you with this from the browser-side.

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染