How to use ajax in Codeigniter view

Welcome to GetSolved !! again .. today we will see how we can “execute queries under the hood” by using ajax in the codeigniter View ..

couple of things may need to add in different secitons. eg. model , controller etc .

// model 

	public function fetch_category(){

		$this->load->model('LabModel');

		$this->LabModel->test_fetch();


		
	}

// view 

       <select class="form-control" id="categoryname" name="categoryname">

       </select>

// javascript 

	$.get("http://localhost/codyii/index.php/lab/fetch_category",{testcategory: 'test'},function(data,status){
		
		$('#categoryname').html(data);
		alert(data);

	});

// controller 

	public function test_fetch(){

		$query = $this->db->get('test_category_entry');
		
		$option = $query->result_array();

		$data = "<select>";
		foreach ($option as $key => $value) {
			$data .= "<option> ". $value['category_name'] . "</option>";
		}
		$data .= "</select>";
		echo $data;

	}

Advertisements