2011-08-29 119 views
3

我有MySQL表看上去就像是下拉(选择菜单)的问题

区表

id  | region 
------------------- 
1  | Region1 
2  | Region2 

...

和学校表

region_id | school 
------------------- 
1   | schno1 
1   | schno5 
1   | schno6 
2   | scho120 

我的页面就像即:首先,页面填充#regions从名为“regions”的数据库表中选择菜单。当用户选择#区域时,js将选定区域的值发送到search.php。 Serverside php脚本在#region(以前选择的菜单)值中搜索名为“schools”的db表,查找所有匹配并回显它们。

现在的问题是,如何隐藏#class和#school选择菜单,并只显示错误消息“在该地区找不到学校”如果找不到匹配项?如何检查search.php没有结果?我是js的新手。如果可能的话,请编辑我的代码。 Thx提前。对不起,我的英语不好

我的JavaScript看起来像http://pastie.org/2444922,并从形式http://pastie.org/2444929的码和最后的search.php http://pastie.org/2444933

UPDATE 我改变了我的js,但没有成功!

$(document).ready(function(){ 
    $("#school").hide(); 
    $("#class").hide(); 
searchSchool = function(regionSelect){ 
var selectedRegion = $("select[name*='"+regionSelect.name+"'] option:selected").val(); 
if (selectedRegion!='0'){ 
    $.ajax({ 
    type: "POST", 
    url : "core/code/includes/search.php", 
    data: "&region_id="+selectedRegion, 
    success: function(result, status, xResponse){ 
     if (result!=null){ 
      $("#school").show(); 
      $("#class").show(); 
      $("#school").html(result); 
     }else{ 
      $("#error").html("There is no school found in this region"); 
      $("#school").html(''); 
      $("#school").hide(); 
     } 
    }, 
    error: function(e){ 
     alert(e); 
    } 
    }); 
}else{ 
    $("#error").html('Please select a region first'); 
    $("#school").html('');   
    $("#school").hide(); 
    $("#class").hide(); 
} 
} 
}); 
+1

如果您只是想隐藏像你的选择菜单的元素,看看这个:http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/ –

+0

@Knix。首先thx快速回复。不,我想检查发送的查询是否没有结果,然后隐藏这两个依赖菜单并仅显示错误消息。请阅读我的代码。 –

+0

请问您可以通过Skype或其他即时通讯与我聊天吗?我真的需要帮助。我的Skype:tural.teyyuboglu –

回答

1

你可以试试这个

的index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
    <title>Ajax With Jquery</title> 

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script> 
    <script type="text/javascript"> 
    searchSchool = function(regionSelect){ 
    var selectedRegion = $("select[name*='"+regionSelect.name+"'] option:selected").val(); 
    if (selectedRegion!='0'){ 
     $.ajax({ 
      type: "POST", 
      url : "search.php", 
      data: "&region_id="+selectedRegion, 
      success: function(result, status, xResponse){ 
      alert(result); 
      if (result!=''){ 
        $("#school").show(); 
        $("#school").html(result); 
       }else{ 
        $("#error").html("There is no school found in this region"); 
        $("#school").html(''); 
        $("#school").hide(); 
       } 
      }, 
      error: function(e){ 
       alert(e); 
      } 
     }); 
    }else{ 
     $("#error").html('Please select a region first'); 
     $("#school").html('');   
     $("#school").hide(); 
    } 
} 
</script> 
</head> 


<body> 

    <?php 
$username="root"; 
$password=""; 
$database="test"; 

mysql_connect('localhost',$username,$password); 
@mysql_select_db($database) or die("Unable to select database"); 
$query="SELECT * FROM regions"; 
$result=mysql_query($query); 

$num=mysql_numrows($result); 

mysql_close(); 

echo "<b><center>Database Output</center></b><br><br>"; 

?> 
<select name="region" id="region" onchange="searchSchool(this)"> 
<option value="0">Please select a Region</option> 
<?php 
while($data = mysql_fetch_array($result)) 
{ 
?> 
<option value="<?php echo $data['id']?>"><?php echo $data['name']?></option> 
<?php 
} 
?> 
</select> 

<select name="school" id="school"></select> 

<span id="error"></span> 

</body> 
</html> 

的search.php:

<?php 

$username="root"; 
$password=""; 
$database="test"; 

mysql_connect('localhost',$username,$password); 
@mysql_select_db($database) or die("Unable to select database"); 

if(isset($_POST['region_id'])) { 
$query = "SELECT * FROM schools WHERE region_id='".$_POST['region_id']."'"; 
$result=mysql_query($query); 

$num = mysql_numrows($result); 

if ($num>0){ 
    while ($row = mysql_fetch_array($result)) { 
     echo '<option value="'.$row['id'].'">'.$row['name'].'</option>'; 
    } 
    } 
else{ 
return null; 
} 
} 


mysql_close(); 
?> 
+0

thx非常快的答复。如果可能的话,我们可以通过Skype聊天吗?我的Skype名称tural.teyyuboglu请加我 –

+0

它不适合我。请加我到你的Skype名单..我想和你谈谈 –

+0

你能在这里发布错误吗? –

1

那么我可以不完全通过你完整的代码读取,但我可以给你模拟了你可能想做的事情。

有包裹在一个div依赖下拉菜单两种。

<div id="dep1"></div> 
<div id="dep2"></div> 

现在在服务器端进行验证后,如果您发现元素创建一个下拉并发送它或只是发送一条错误消息。

<? 
if($num>0) { 
?> 
<select> 
<? 
foreach($element as $ele) { 
<option><?=$ele?></option> 
} 
?> 
</select> 
<? 
} else { 
?> 
<div class="error">No regions found</div> 
<? } ?> 

你的JS会看起来像

$("#dep1").html(loadbar).load("mypage.php","region="+regionid); 
+0

请问您可以通过Skype或其他即时通讯与我聊天吗?我真的需要帮助。我的Skype:tural.teyyuboglu –

+0

已更新的问题。请看看 –

1

我认为这个问题存在于上线27-30 jQuery代码。没有ID = cl_dropdown的元素,分隔符使得它在sch_dropdown中查找cl_dropdown。

我的猜测是,第二选择用于具有ID cl_dropdown在一个点上。如果是这样,它的HTML应该看起来像这样:

<select id="cl_dropdown" name="class"> 

你还应该有一个消息的元素。根据jQuery,你应该有一个#no_sch元素,但我没有看到它。

<div id="no_sch"></div> 

然后替换线27-30下列要求:

if (!results)  { 
    $("#sch_dropdown").hide(); 
    $("#cl_dropdown").hide(); 
    $('#no_sch').show(); 
    $('#no_sch').text('no matches found'); 
}; 
+0

请问您可以通过Skype或其他即时通讯与我聊天吗?我真的需要帮助。我的Skype:tural.teyyuboglu –

+0

更新了问题。请看看 –

+0

嗨。它看起来已经解决了。 –