简体   繁体   English

基于php查询的级联下拉列表

[英]Cascading dropdowns based on a php query

I have a dropdown which contains each day of the week. 我有一个下拉菜单,其中包含一周中的每一天。

DAYS[Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday] 

I then run this query: 然后,我运行以下查询:

"SELECT DISTINCT DAY_1 FROM credit_three"

and I put the results in another drop-down, using the following code: 然后使用以下代码将结果放入另一个下拉列表中:

<select name='DAY_1' onchange="ILovePHP();">
    <?php while ($row = mysql_fetch_array($result)) {
        echo"<option value='" .$row['DAY_1']."'>". $row['DAY_1']."</option>";
    }
</select>

When the value in the dropdown changes, I need to run another query. 当下拉列表中的值更改时,我需要运行另一个查询。 To do so, I have tried the following: 为此,我尝试了以下方法:

  1. On-Change I am running the function ILovePHP() , which is provided by the script: 更改时,我正在运行函数ILovePHP() ,该函数由脚本提供:

    b= alert(b); b =警报(b);

myfunction() was written in php like myfunction()是用php编写的,例如

function myfunction() {
    $sql = "SELECT TIME_1 FROM CREDIT_THREE WHERE DAY_1=DAY1";
    $result = mysql_query($sql2);
    row = mysql_fetch_array($result);
 }

But... my query isn't running. 但是...我的查询没有运行。 What am I doing wrong? 我究竟做错了什么?

You have a typo - you are putting your query into a variable called $sql but then passing a different variable to mysql_query , " $sql2 " 您有一个错字-您将查询放入名为$sql的变量中,然后将另一个变量传递给mysql_query$sql2

$sql="SELECT TIME_1 FROM CREDIT_THREE WHERE DAY_1=DAY1";
$result=mysql_query($sql2);

You need to use AJAX to run the server-side function. 你需要使用AJAX来运行服务器端的功能。

Try this: 尝试这个:

<script>
function myfunction(str2) {
    var xmlhttp;
    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("myDiv").innerHTML = xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET", "ajax_info.php?q=" + str2, true);
    xmlhttp.send();
}
</script>

To set the value of str2 , you need to pass in this.value 要设置str2的值,您需要传递this.value

In the ajax_info.php page, you can now code your query and the result will be shown to you in the div tag with id="mydiv" . 现在,在ajax_info.php页面中,您可以对查询进行编码,结果将在div标签中以id="mydiv"显示给您。 You can do this anywhere in your front-end page, as follows: 您可以在前端页面的任何位置执行此操作,如下所示:

<div id="mydiv"></div>

Now you can use the line: 现在您可以使用以下行:

echo"<option value='" .$row['DAY_1']."'>". $row['DAY_1']."</option>";

in your ajax_info.php page, and get the result you want in the div tag. 在您的ajax_info.php页面中,并在div标签中获得所需的结果。

Let me know if you need any more help. 让我知道您是否需要更多帮助。

You need to send ajax request on change of dropdown and this ajax request will get the result from query and create html and then you need to replace html with jquery. 您需要在下拉列表更改时发送ajax请求,此ajax请求将从查询中获取结果并创建html,然后需要用jquery替换html。 For detail you can see the following link.. 有关详细信息,请参见以下链接。

http://www.plus2net.com/php_tutorial/ajax_drop_down_list-demo.php http://www.plus2net.com/php_tutorial/ajax_drop_down_list-demo.php

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM