php 商务网站购物车联动地址
數據表如下:
CREATE TABLE IF NOT EXISTS `china` (
`region_id` smallint(5) unsigned NOT NULL,
? `parent_id` smallint(5) unsigned NOT NULL DEFAULT '0',
? `region_name` varchar(120) NOT NULL DEFAULT '',
? `region_type` tinyint(1) NOT NULL DEFAULT '2',
? `agency_id` smallint(5) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM? DEFAULT CHARSET=utf8;
?數據見文件:
測試頁test.php,
在這個文件得先引入 jquery文件和相應的js文件(accoun_1.js),
用 ajax技術查找(ajax_1.php)
test.php文件如下
<?php
session_start();
$link = mysql_connect('localhost', 'root', '123456') or die("Error: " . mysql_error());
mysql_select_db('trade', $link);
mysql_query("set names utf8", $link);
$province = array();
$sql = "select * from china where parent_id = 1";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
?? ?$province[$row['region_id']] = $row;
}
?>
<script src="jquery-1.10.2.js" type="text/javascript"></script>
<script src="accoun_1.js" type="text/javascript"></script>
<center>
<form name="myform" action="deal.php" method="post">
<input type="hidden" value="ajax_1.php" id="site_url">
?? ?<div id="box">
?????? ??? ??? ?<div class="tr china len2">
?????? ??? ??? ??? ?<span><b></b>省份:</span>
?????? ??? ??? ??? ?<select name="province" class="province option">
?????? ??? ??? ??? ??? ?<option>-請選擇省-</option>
??????????????????????? <?php foreach ($province as $key => $value):?>
?????? ??? ??? ??? ??? ?<option><?php echo $value['region_name'];?></option>
??????????????????????? <?php endforeach;?>
?????? ??? ??? ??? ?</select>
?????? ??? ??? ??? ?<select name="city"? class="city option">
?????? ??? ??? ??? ??? ?<option>-請選擇市-</option>
?????? ??? ??? ??? ?</select> ?
??????? ??? ??? ??? ?<select name="county"? class="county option">
?????? ??? ??? ??? ??? ?<option>-請選擇區/縣-</option>
?????? ??? ??? ??? ?</select>????? ??? ??? ??? ????? ??? ??? ??? ?
?????? ??? ??? ?</div>
?????? ??? ??? ?<div class="address len2">
?????? ??? ??? ??? ?<span><b></b>地址:</span>
?????? ??? ??? ??? ?<textarea name="address"></textarea>
?????? ??? ??? ?</div>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</center>
acoun_1.js文件如下:
// id = 1為省份,2為城市
$(document).ready(function(){
?? ?var Iprovince=$('#box .china .province');
?? ?var Icity=$('#box .china .city');
?? ?var Icounty=$('#box .china .county');
?? ?var address = $('#box .address textarea');
?? ?Iprovince.change(function(){
?? ??? ?var province = $(this).val();
?? ??? ?address.text(province);
?? ??? ?var _url = 'ajax_1.php';
?? ??? ?$.ajax({
?? ??? ??? ?url:_url,
??????????? type: 'post',
??????????? data:{'prov': province, 'id': 1},
??????????? success:function(data)
?? ??? ??? ?{
?? ??? ??? ?? Icity.append(data);
?? ??? ??? ?}
?? ??? ?});
?? ?});?? ?
?? ?Icity.change(function(){
?? ??? ?var city = $(this).val();
?? ??? ?address.text(address.text()+city);
?? ??? ?var _url = 'ajax_1.php';
?? ??? ?$.ajax({
?? ??? ??? ?url:_url,
?? ??? ??? ?type:'post',
?? ??? ??? ?data:{'city':city, 'id': 2},
?? ??? ??? ?success:function(data)
?? ??? ??? ?{
?? ??? ??? ??? ?Icounty.append(data)
?? ??? ??? ?}
?? ??? ?});
?? ??? ?
?? ?});
?? ?Icounty.change(function(){
?? ??? ?var country = $(this).val();
?? ??? ?address.text(address.text()+country);
?? ?})
});
ajax_1.php文件如下:
<?php
session_start();
$link = mysql_connect('localhost', 'root', '123456') or die("Error: " . mysql_error());
mysql_select_db('trade', $link);
mysql_query("set names utf8", $link);
// 1 表省份 2 表城市
if($_POST['id'] == 1)
{
?? ?$region_name = $_POST['prov'];
?? ?$city = '';
?? ?$sql = "select * from china where region_name = '{$region_name}'";
?? ?$result = mysql_query($sql);
?? ?$pro_row = mysql_fetch_assoc($result);
?? ?$pro_id = $pro_row['region_id'];
?? ?
?? ?$query = "select * from china where parent_id = '{$pro_id}'";
?? ?$res = mysql_query($query, $link);
?? ?while($row = mysql_fetch_assoc($res))
?? ?{
?? ??? ?$city .= '<option>' . $row['region_name'] . '</option>';
?? ?}
?? ?echo $city;
?? ?
}else if($_POST['id'] == 2)
{
?? ?$region_name = $_POST['city'];
?? ?$country = '';
?? ?$sql = "select * from china where region_name = '{$region_name}'";
?? ?$result = mysql_query($sql);
?? ?$pro_row = mysql_fetch_assoc($result);
?? ?$pro_id = $pro_row['region_id'];
?? ?
?? ?$query = "select * from china where parent_id = '{$pro_id}'";
?? ?$res = mysql_query($query, $link);
?? ?while($row = mysql_fetch_assoc($res))
?? ?{
?? ??? ?$country .= '<option>' . $row['region_name'] . '</option>';
?? ?}
?? ?echo $country;
}
?>
轉載于:https://www.cnblogs.com/lin3615/p/3817368.html
總結
以上是生活随笔為你收集整理的php 商务网站购物车联动地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot2.0+中,自定义
- 下一篇: 【题解】quake