php - 下拉(选择菜单)问题

标签 php jquery mysql html drop-down-menu

我的 MySQL 表看起来像那样

地区表

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

...

和学校表

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

我的页面是这样工作的:首先,页面从名为“regions”的数据库表中填充#regions 选择菜单。当用户选择#region 时,js 将所选区域的值发送到 search.php。服务器端 php 脚本在名为“schools”的数据库表中搜索 #region(先前选择的菜单)值,找到所有匹配项并回显它们。

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

我的 javascript 看起来像那样 http://pastie.org/2444922和表格 http://pastie.org/2444929 中的一段代码最后是 search.php http://pastie.org/2444933

更新 我改变了我的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();
}
}
});

最佳答案

你可以试试这个

索引.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>

搜索.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();
?>

关于php - 下拉(选择菜单)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7226777/

相关文章:

php - 按角色限制 woocommerce 订单状态

php - 后端如何在 Zend Framework 中查找 AjaxLink() 调用

php - 基于多个动态值的Where子句,CodeIgniter

javascript - 从 jQuery .load() 检索 URL

php - 使用一张表将不同的 MIME 类型上传到数据库

php - 如何解决尝试在 Laravel 中获取非对象的属性?

php - 算法问题 : select two stories per topic so that the same story is never selected for two different topics

php - 如何在一行中显示 3 个 <li>

php - 使用 jQuery 幻灯片从文件夹中获取图像

php - 下拉菜单 mysql PHP