加载页面后没有显示任何内容

时间:2013-12-07 05:34:29

标签: php mysql

我有两个下拉列表。 drop1_1和tier_two。 Drop_1选择所有app_cn,tier_two选择app_plan_no

counter | app_cn   | app_plan_no 

000004  |  comp1   |  1
000172  |  comp1   |  1
000007  |  comp1   |  2
000005  |  comp2   |  1

当我加载页面时,没有显示任何内容。当我的drop_1下拉列表中的选定值为ALL时,我需要显示所有记录。如果我选择comp1,则在tier_two下拉列表中选择之前不会显示任何内容。我的查询确实存在问题。任何人吗?

Getuser.php

$mysqli = new mysqli("localhost", "root", "", "app");
$p = $_GET['p'];
$q = $_GET['q'];
$where = '';
if ( $q != 'ALL') {
    $where = " WHERE app_cn='$q' AND app_plan_no='$p'  ";
$result1 = $mysqli->query("
    SELECT *
    FROM app 
    $where 
    GROUP BY counter
")or die(mysqli_error());
echo'<table>'
.......

这是我的完整代码

Combobox.php

<script> // AJAX Implementation
function showUser() {
    str = document.getElementById("drop_1").value;
    str1 = document.getElementById("tier_two").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", "getuser.php?q=" + str + "&p=" + str1, true);
    xmlhttp.send();
}

</script>
<body onload=showUser(str="ALL")>

<?php include('func.php'); ?>
<select name="drop_1" id="drop_1" onchange="showUser()" style="overflow:scroll;width:100px;">
        <option value="ALL" selected='ALL'>ALL</option>
        <?php getTierOne(); ?>
</select>

func.php

<?php

function getTierOne()
{
    $mysqli = new mysqli("localhost", "root", "", "app");
    $result = $mysqli->query("SELECT * FROM app GROUP BY app_cn ORDER BY app_cn");
     while($row = $result->fetch_assoc())
        {
           echo '<option value="'.$row['app_cn'].'">'.$row['app_cn'].'</option>';
        }
}

if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
   drop_1($_GET['drop_var']); 
}

function drop_1($drop_var)
{
    $mysqli = new mysqli("localhost", "root", "", "app");
    $results = $mysqli->query("SELECT * FROM app WHERE app_cn='$drop_var' GROUP BY app_plan_no ORDER BY app_plan_no");

    echo '<select name="tier_two" id="tier_two" onchange="showUser()">
          <option value=" " disabled="disabled" selected="selected">Choose one</option>';

          while($drop_2 = $results->fetch_assoc())
            {
            if($drop_2['app_plan_no'] != '')
            {
              echo '<option value="'.$drop_2['app_plan_no'].'">'.$drop_2['app_plan_no'].'</option>';
            }
            }
    echo '</select> ';
}
?>

2 个答案:

答案 0 :(得分:2)

而不是

<body onload=showUser(str="ALL")>

你应该使用

<body>
<!-- all your html goes here -->
</body>
<script>
    showUser();
</script>

body onload触发drop_1并且tire_two未创建时,破坏只会引发异常。

修改

首次加载或q=ALL时,您无需发送第二个参数p。所以改变

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

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

修改2:修改后的Combobox.php

<script> // AJAX Implementation
function showUser() {
    str = document.getElementById("drop_1").value;
    str1 = document.getElementById("tier_two").value;
    if (str == "" || (str != "ALL" && 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", "getuser.php?q=" + str + "&p=" + str1, true);
    xmlhttp.send();
}
</script>
<body>

<?php include('func.php'); ?>
<select name="drop_1" id="drop_1" onchange="showUser()" style="overflow:scroll;width:100px;">
        <option value="ALL" selected='ALL'>ALL</option>
        <?php getTierOne(); ?>
</select>
<!-- your other html... -->
</body>

<script>
    showUser();
</script>

编辑3:

这是一个疯狂的猜测,可能这就是你想要的

func.php

中的更改
if(isset($_GET['func']) && $_GET['func'] == "drop_1") {
   drop_1($_GET['drop_var']); 
}else{
   drop_1(''); // when you call the func.php first time $_GET['drop_var'] is not set so call with blank drop_var
}

function drop_1($drop_var)
{
    $mysqli = new mysqli("localhost", "root", "", "app");
    $query = "SELECT * FROM app WHERE app_cn='$drop_var' GROUP BY app_plan_no ORDER BY app_plan_no";
    if (empty($drop_var)){
        //if the drop_var is empty select all records
        $query = "SELECT * FROM app GROUP BY app_plan_no ORDER BY app_plan_no";
    }
    $results = $mysqli->query($query);

    echo '<select name="tier_two" id="tier_two" onchange="showUser()">
          <option value=" " disabled="disabled" selected="selected">Choose one</option>';

          while($drop_2 = $results->fetch_assoc())
            {
            if($drop_2['app_plan_no'] != '')
            {
              echo '<option value="'.$drop_2['app_plan_no'].'">'.$drop_2['app_plan_no'].'</option>';
            }
            }
    echo '</select> ';
}

答案 1 :(得分:0)

这是javascript中的语法错误:

<body onload=showUser(str="ALL")>

应该是:

<body onload=showUser("ALL")>

Javascript在调用时不支持命名参数。此外,您应该将onchange更改为:onchange="showUser(this)"并将函数定义更改为:

function showUser(elem) {
   console.log(elem.value); // now you don't have to do all the document.getElementById stuff
}