2
我的控制器: -笨:依賴下拉菜單無法正常工作
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Cnewincident extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function index()
{
$this->load->model('users/supervisor/Mnewincident');
$data['category'] = $this->Mnewincident->getCatgories();
//loads up the view with the query results
$this->load->view('users/supervisor/vnewincident',$data);
if($this->input->post('addrequest'))
{
$this->load->model('users/supervisor/Mnewincident');
// to get the username from table who is creating this request
$session_data = $this->session->userdata('emailid');
$firstname = $this->Mnewincident->get_firstname($session_data);
// passing that usename in newincident table
if($this->Mnewincident->add_request($firstname))
{
$this->session->set_flashdata('message', 'Request added successfully.');
redirect('users/supervisor/Cnewincident');
}
else
{
$this->session->set_flashdata('message', 'Failed to add the request. Try again');
redirect('users/supervisor/Cnewincident');
}
}
}
//call to fill the second dropdown with the subcategories
public function loadsubcat()
{
echo $category_id = $this->input->post('id',TRUE);
$subcatData['subcatDrop']=$this->Mnewincident->getSubcatgories($category_id);
$output = null;
foreach ($subcatData['subcatDrop'] as $row)
{
$output .= "<option value='".$row->subcategory_description."'>".$row->subcategory_description."</option>";
}
echo $output;
}
}
?>
我的模型: -
class Mnewincident extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function getCatgories()
{
$this->db->select('category_id,category_description');
$this->db->from('tbl_categorymaster');
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['category_id']]=$row['category_description'];
}
return $data;
}
public function getSubcatgories($category_id=string)
{
$this->db->select('srno,subcategory_description');
$this->db->from('tbl_subcategorymaster');
$this->db->where('category_id',$category_id);
$query = $this->db->get();
return $query->result();
}
}
我的視圖代碼: -
$(document).ready(function() {
$("#category").change(function(){
$.ajax({
url:"<?php echo base_url()."users/supervisor/Cnewincident/loadsubcat"?>",
data: {id:$(this).val()},
type: "POST",
success:function(data){
$("#subcategory").html(data);
}
});
});
});