shank shank -4 years ago 132
MySQL Question

calling controller function from view and retrieving rows from db using AJAX in codeigniter

I am trying to retrieve rows from database by making an AJAX call to the controller function. I have no error messages and the code is executing properly upto $("#msgbox").html("Type the name of someone or something...");

I think the controller is not called properly from the view. Below is my MVC code.

My view: welcome_message.php

<div id="centersearch">
<script type="text/javascript" src="
<script type="text/javascript">
var start=/^((?!PART).)*$/
var word=/^((?!PART).)*$/

var content=$(this).text(); //Content Box Data
var go= content.match(start); //Content Matching @
var name= content.match(word); //Content Matching @abc
var dataString = 'searchword='+ name;
//If @ available
$("#msgbox").html("Type the name of someone or something...");
//if @abc avalable
type: "POST",
url: "<?php $this->load->helper('url'); echo base_url();?>index.php/Welcome/ ?>", // Database name search
data: dataString,
cache: false,
success: function(data)
return false();

//Adding result name to content box.
var username=$(this).attr('title');
var old=$("#contentbox").html();
var content=old.replace(word," "); //replacing @abc to (" ") space
var E="<a class='red' contenteditable='false' href='#' >"+username+"</a>";

My controller: Welcome.php

public function search()
//load the model
//load the method of model
$this->load->view('welcome_message', $data);


My model: Select.php

public function search($datastring)

$query = $this->db->select('*')
->where('from', $datastring)
return $query;

Answer Source

You can call your helper in your constructor

function __construct() {

And your ajax url is

  url: "<?php echo base_url();?>index.php/Welcome/search?>",
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download