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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s