標籤:
<form action="" method="post" >
<fieldset>
<p>
<label>標題</label>
<input class="text-input small-input" type="text" id="small-input" name="name" />
<!-- <input class="text-input small-input" id="small-input" name="title" />-->
<!--名字儘可能與資料表的欄位名一致-->
<span class="input-notification success png_bg">Successful message</span>
<br />
<small>A small description of the field</small> </p>
<p>
<label>父ID</label>
<input class="text-input small-input" type="text" id="pid" name="pid" readonly="readonly" />
</p>
<p id="Pid">
<select class="text-input small-input" id="pid0">
<option value="0" status="0">頂級</option>
<?php foreach($result as $value){ ?>
<option value="<?php echo $value[‘id‘] ?>" status="1"><?php echo $value[‘name‘] ?></option>
<?php } ?>
</select>
</p>
<p>
<input class="button" type="submit" value="Submit" />
</p>
</fieldset>
<div class="clear"></div>
<!-- End .clear -->
</form>
//ajax交換。
$(function(){
$(‘#Pid select‘).live(‘change‘,function(){
//首先要擷取id
var id=$(this).attr(‘value‘)
//擷取層級。
var level=$(this).attr(‘id‘).substr(3)
$(‘#pid‘).attr(‘value‘,id)
var status=$(this).find(‘option:select‘).attr(‘status‘);
$.ajax({
type:"GET",
url:"index.php?c=category&a=next&admin=1&id="+id,
dataType:"json",
success: function(json){
var next_level=Number(level)+1
var tmp_level=next_level
while($(‘#pid‘+tmp_level).length !=0){
$(‘#pid‘+tmp_level).remove();
tmp_level++
}
if(json.length !=0&&status==1){
var l=‘‘
l+=‘ <select class="text-input small-input" id="pid‘+next_level+‘">‘
for(var i=0;i<json.length;i++){
l+=‘<option value="‘+json[i].id+‘" status="1">‘+json[i].name+‘</option>‘
}
l+=‘</select>‘
$(‘#Pid‘).append(l)
}
}
})
return false
})
})
</script>
//函數的封裝
function getList($perpage,$offset,$table,$con=‘1‘){
$sql="select * from $table where $con order by id desc limit $offset,$perpage";
$query=mysql_query($sql);
$result=array();
while($row=mysql_fetch_assoc($query)){
$result[]=$row;
}
return $result;
}
//php後台控制器
<?php
$id=!empty($_GET[‘id‘])?$_GET[‘id‘]:0;
$arr=getList(100,0,‘category‘,‘pid=‘.$id);//函數調用
if(!empty($_SERVER[‘HTTP_X_REQUESTED_WITH‘])){//$_SERVER是系統內容變數,已經定義好的。
echo json_encode($arr);
}
?>
php中無極分類—>新聞列添加。