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
JavaScript
x
<div id="centersearch">
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
var start=/^((?!PART).)*$/
var word=/^((?!PART).)*$/
$("#contentbox").live("keyup",function()
{
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
if(go.length>0)
{
$("#msgbox").slideDown('show');
$("#display").slideUp('show');
$("#msgbox").html("Type the name of someone or something...");
//if @abc avalable
if(name.length>0)
{
$.ajax({
type: "POST",
url: "<?php $this->load->helper('url'); echo base_url();?>index.php/Welcome/ ?>", // Database name search
data: dataString,
cache: false,
success: function(data)
{
$("#msgbox").hide();
$("#display").html(data).show();
}
});
}
}
return false();
});
//Adding result name to content box.
$(".addname").live("click",function()
{
var username=$(this).attr('title');
var old=$("#contentbox").html();
var content=old.replace(word," "); //replacing @abc to (" ") space
$("#contentbox").html(content);
var E="<a class='red' contenteditable='false' href='#' >"+username+"</a>";
$("#contentbox").append(E);
$("#display").hide();
$("#msgbox").hide();
});
});
</script>
My controller: Welcome.php
JavaScript
public function search()
{
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['s']=$this->select->search();
$this->load->view('welcome_message', $data);
}
My model: Select.php
JavaScript
public function search($datastring)
{
$query = $this->db->select('*')
->from('country')
->where('from', $datastring)
->get();
return $query;
}
Advertisement
Answer
You can call your helper in your constructor
JavaScript
function __construct() {
parent::__construct();
$this->load->helper('url');
}
And your ajax url is
JavaScript
url: "<?php echo base_url();?>index.php/Welcome/search?>",