-2
在我控制我的代碼:如何自動填充json數據中的ajax選擇選項?
<?php
class Cat_cntrl extends CI_controller{
public function __construct(){
parent::__construct();
$this->load->model('cat_model');
$this->load->helper('form','url');
}
public function index(){
$get=$this->cat_model->fetchcatdata();
$data['result']=$get;
$this->load->view('cat_view',$data);
}
public function subcatselect(){
$cate=$this->input->post('cate');
$get=$this->cat_model->getsubo($cate);
$data['result']=$get;
echo json_encode(array('result'=>$data['result']));
}
}
?>
模型我有代碼:
<?php
class Cat_model extends CI_model{
public function fetchcatdata(){
$this->db->select()->from('cat');
$query=$this->db->get();
if($query->num_rows() > 0){
return $query->result();
}
else {
echo "error";
}
}
public function getsubo($cate){
$this->db->select($cate)->from('sub_cat');
$query=$this->db->get();
//echo $this->db->last_query();
if($query->num_rows() > 0){
return $query->result();
}
else {
echo "error";
}
}
}
鑑於我的代碼有:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <title></title>
</head>
<body>
<form method="post" action="">
<table>
<tr>
<td>Category</td>
<td><select id="cate">
<option>
None Selected
</option>
<?php foreach ($result as $value) {
?>
<option id="val">
<?php echo $value->category; ?>
</option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Sub Category</td>
<td><select id="myselect">
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" name="" id="sub">
</td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
$('#sub').click(function(e){
e.preventDefault();
var cate=$("#cate option:selected").val();
var url="<?php echo base_url();?>cat_cntrl/subcatselect";
$.ajax({
type:'post',
url:url,
data:{cate :cate},
success:function(response){
}
})
})
})
</script>
的問題是如何在此代碼中通過ajax自動填充選擇。成功後使用什麼功能?我沒有從我的情況中得到Google的確切答案。
不工作..阿克沙伊 – nisha
@nisha我修改我的職務,並增加了一個例子也給你從頭開始 –