加载页面后无显示 [英] Nothing display after I load the page
问题描述
我有两个下拉菜单. drop1_1和tier_two. Drop_1选择所有app_cn,tier_two选择app_plan_no
I have two dropdown. drop1_1 and tier_two. Drop_1 select all app_cn and tier_two selects app_plan_no
counter | app_cn | app_plan_no
000004 | comp1 | 1
000172 | comp1 | 1
000007 | comp1 | 2
000005 | comp2 | 1
当我加载页面时,没有任何显示.当我的drop_1下拉列表中的Selected值为ALL时,我需要显示所有记录.而且,如果我选择comp1,则直到在tier_two下拉列表中选择之前,什么都不会出现.我的查询中确实有问题.有人吗?
When I load the page nothing displays. I need to display all records when Selected value in my drop_1 dropdown is ALL. And if I select comp1 nothing appears until I select in tier_two dropdown. I'm really having problem in my query. Anyone?
Getuser.php
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
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> ';
}
?>
推荐答案
而不是
<body onload=showUser(str="ALL")>
您应该使用
<body>
<!-- all your html goes here -->
</body>
<script>
showUser();
</script>
当body onload
触发器drop_1
和tire_two
未被创建时,scsc只会引发异常.
When body onload
triggers drop_1
and tire_two
are not created and the scrupt will just throw an exception.
在第一次加载时或在q=ALL
时,无需发送第二个参数p
.所以改变
When first loading or if your q=ALL
, you don't need to send the second parameter p
. so change
if (str == "" || str1 == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
收件人
if (str == "" || (str != "ALL" && str1 == "")) {
document.getElementById("txtHint").innerHTML = "";
return;
}
已修改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:
这是一个疯狂的猜测,可能正是您想要的
This is a wild guess and may be this is what you want
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> ';
}
这篇关于加载页面后无显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!