简体   繁体   English

如何使用ajax基于两个下拉列表的值从数据库检索数据?

[英]how to retrieve data from database based on the value of two drop down list using ajax?

i have implemented the Example . 我已经实现了示例 Here i am retrieving data using only one drop down list. 在这里,我仅使用一个下拉列表检索数据。 Its working fine. 它的工作正常。 But if I want to do the same for two drop down list that is values from database have to be retrieved based on the value of two drop down list. 但是,如果我想对两个下拉列表执行相同的操作,则必须基于两个下拉列表的值来检索数据库中的值。 I am trying as follows: Ajax script- 我正在尝试如下:Ajax脚本-

<script> // AJAX Implementation
function showCourses() {
    str = document.getElementById("branch").value;
    str1 = document.getElementById("sem").value;
    if (str == "" || str1 == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "listCourseByAjax.php?p=" + str + "&q=" + str1, true);
    xmlhttp.send();
}
</script>

drop down lists- 下拉列表-

<select name="branch" onchange="showCourses(this.value)">
        <option id="branch" value="0" selected>Select one</option>
            <option id="branch" value="ISE">1/option>
            <option id="branch" value="CSE">2</option>
            <option id="branch" value="ME">3</option>
</select>
<select name="sem" onchange="showCourses(this.value)">
    <option id="sem" value="0" selected>select one</option>
    <option id="sem" value="I-P">I-P</option>
    <option id="sem" value="I-C">I-C</option>
    <option id="sem" value="II-P">II-P</option>
</select>

php file- PHP文件-

$p = $_GET['p'];
$q = $_GET['q'];
$sql="SELECT * FROM course_details WHERE sem='" . $q . "' AND branch='" . $p . "'";
$result = mysql_query($sql);

echo "<table border='1'>
<tr>
    <th>Course Code</th>
    <th>Course Name</th>
    <th>Course Instructor</th>
    <th>Credit</th>
</tr>";

Please help me how to do it. 请帮我怎么做。

There is an issue with how you are referencing the selected values of the SELECT elements, or more accurately, how your SELECT elements are structured. 如何引用SELECT元素的选定值,或更准确地说,如何构造SELECT元素存在问题。 You need to place your ID (branch and sem) on the SELECT and not the individual options and you can remove the this.value parameter when you call showCourses() onchange because your function isn't set up to accept a parameter. 您需要将ID(分支和sem)放在SELECT上,而不是在单个选项上,并且在调用showCourses()onchange时可以删除this.value参数,因为您的函数未设置为接受参数。

Try using this for your SELECTs 尝试将此用于您的SELECT

<select name="branch" id="branch" onchange="showCourses()">
  <option value="0" selected>Select one</option>
  <option value="ISE">1</option>
  <option value="CSE">2</option>
  <option value="ME">3</option>
</select>
<select name="sem" id="sem" onchange="showCourses()">
  <option value="0" selected>select one</option>
  <option value="I-P">I-P</option>
  <option value="I-C">I-C</option>
  <option value="II-P">II-P</option>
</select>

I also noticed that your check for selected values is looking for an empty string for either str or str1, but your "Select One" options have a value of 0 so you may want to change: 我还注意到,您检查的选定值正在寻找str或str1的空字符串,但是您的“ Select One”选项的值为0,因此您可能需要更改:

if (str == "" || str1 == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
}

To

if (str == "0" || str1 == "0") {
    document.getElementById("txtHint").innerHTML = "";
    return;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用下拉列表中的选定值显示数据库中的列表 - How to display the list from database using selected value in drop down 根据下拉列表选择显示数据库中的值 - Display a value from database based on Drop down list selection 如何基于级联下拉列表选择的值从数据库中获取属性值? - How to get a property value from database based on cascaded drop down list selected values?? 如何从Ajax的级联下拉列表中获取价值 - How get value from cascading drop down list made with ajax 如何从其他页面的Ajax下拉列表中检索值? PHP - How do you retrieve values from an Ajax drop down list from a different page? PHP 使用AJAX获取PHP文件以从MySQL数据库检索数据时,下拉表单消失 - Drop down form dissapearing when using AJAX to get PHP file to retrieve data from MySQL db 使用下拉列表从mysql数据库中获取数据,然后使用搜索功能从选定的下拉列表选项中过滤数据 - Fetch data from mysql database using drop down list and then use search function to filter data from selected drop down list option 如何使用敲除从下拉列表中获取选定的值 - How to get selected value from drop down list using knockout 使用下拉列表从数据库获取数据 - Getting the data from database using a drop down 基于表单输入值,2如何生成选择4 4下拉菜单w从数据库中行/记录数据,将在选定时预填充表单? - Based on a forms input value, how 2 generate selections 4 a drop down menu w row/record data from a database that will prefill a form when selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM