当前位置:编程学习 > JS >>

jquery+php+mysql二级联动的例子带数据库

 jquery+php教程+mysql教程二级联动的例子带数据库教程
这是一款利用juqery 与php实时交互的城市二级联动菜单哦,以前我们做联动二级菜单都是用网页特效现,现在我们就用jquery+php+mysql数据库来实例吧。
-->

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="http://www.zzzyk.com/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
 <script type="text/网页特效" src="jquery.网页特效"></script> 
<script type="text/网页特效">   
$(document).ready(function() {  
  //get provinces  
  $.get("getcontent.php", {category:'province'},  
    function(data) {  
      $('#province').html(data);  
  });  
 
  //get citys  
  $.get("getcontent.php", {category:'city'},  
    function(data) {  
      $('#city').html(data);  
    });  
 
    //province onchange  
    $('#province').change(function() {  
      var province = $(this).val();  
      $.get("getcontent.php", {category:'city', province:province}, function(data) {  
        $('#city').html(data);  
      });  
    });  
 
});  
</script> 
 
<title>二级联动示例</title> 
</head> 
<body> 
 
<form action="check.php" method="post">    
  <select name="province" id="province">    
  </select> 
  <select name="city" id="city" style="width:100px;">    
  </select> 
  <input type="submit" value="submit now" />
</form> 
</body> 
</html> 

getcontent.php代码

<?php
  //author:厦门飞鱼
  //qq:710788018
  //email:tochenwei@163.com 
  define(host, 'localhost');  
  define(user, 'root');  
  define(pw, '');  
  define(db, 'test');  
    
  $connect = mysql_connect(host, user, pw)  
  or die('could not connect to mysql server');  
 
  mysql_select_db(db,$connect)  
  or die('could not select database.');  
  //设置查询编码,不设查询时易出现乱码  
  mysql_query('set names utf8;');  
 
  switch($_request['category']) {  
    //显示数据库中所有省份  
    case 'province':  
        $str = "<option value='0'>请选择省份</option>";  
        $sql = "select * from province";  
        $result = mysql_query($sql) or die (mysql_error());  
          
        if (mysql_num_rows($result) > 0) {  
          while ($row = mysql_fetch_array($result)) {  
            //print_r($row);  
            $str .= "<option value='".$row['id']."'>".$row['name']."</option>";  
          }  
        }  
        echo $str;  
        mysql_free_result($result);  
        break;  
 
    //显示城市  
    case 'city':  
        $str = "<option value='0'>请选择城市</option>";  
        if($_request["province"] != "") {  
           //根据省份得到城市  
        $sql = "select * from city where province_id=".$_request['province'];  
        $result = mysql_query($sql) or die (mysql_error());  
 
        if (mysql_num_rows($result) > 0) {  
          while ($row = mysql_fetch_array($result)) {  
            $str .= "<option value='".$row['id']."'>".$row['city_name']."</option>";  
          }  
        }  
        mysql_free_result($result);  
        }//end of if  
        echo $str;  
        break;  
          
  }//end of switch  
 


  /*
  综合上面的实例我们就可以看得,其实这就是php+ajax实例的无刷新二级菜单了,只是使用的方法不一样罢了。
  */
 
?> 

city.sql
use `test`;

/*table structure for table `city` */

drop table if exists `city`;

create table `city` (
  `id` int(4) not null auto_increment,
  `city_name` varchar(255) not null,
  `province_id` int(4) not null,
  primary key  (`id`)
) engine=innodb default charset=utf8;

/*data for the table `city` */

insert  into `city`(`id`,`city_name`,`province_id`) values (1,'福州',1),(2,'厦门',1),(3,'龙岩',1),(4,'漳州',1),(5,'深圳',2),(6,'广州',2);

provice.sql
create database /*!32312 if not exists*/`test` /*!40100 default character set utf8 */;

use `test`;

/*table structure for table `province` */

drop table if exists `province`;

create table `province` (
  `id` int(4) not null auto_increment,
  `name` varchar(255) not null,
  primary key  (`id`)
) engine=innodb default charset=utf8;

/*data for the table `province` */

insert  into `province`(`id`,`name`) values (1,'福建省'),(2,'广东省');

补充:网页制作,jquery
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,