vendredi 30 janvier 2015

search live Ajax in PHP CI doesnt print the result


I am wondering why in this code, when I type the names in input,There is no result. I also see the error Json parser in my console :



SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data


The problem can be from json file which is sent by model and can not be retrieved with view!or something else from json!


view:index.php



<script>
$(document).ready(function(){
$("#search").keyup(function(){
if($("#search").val().length>3){
$.ajax({
type: "post",
url: "/phpAmir_contract/index.php/employee",
cache: false,
data:'search='+$("#search").val(),
success: function(response){
$('#finalResult').html("");
var obj = JSON.parse(response);
if(obj.length>0){
try{
var items=[];
$.each(obj, function(i,val){
items.push($('<li/>').text(val.FIRST_NAME + " " + val.LAST_NAME));
});
$('#finalResult').append.apply($('#finalResult'), items);
}catch(e) {
alert('Exception while request..');
}
}else{
$('#finalResult').html($('<li/>').text("No Data Found"));
}

},
error: function(){
alert('Error while request..');
}
});
}
return false;
});
});
</script>
</head>
<body>
<div id="container">
<p>Note:- Please start typing surname as "Chavan", "Patil"</p>
<input type="text" name="search" id="search" />
<ul id="finalResult"></ul>
</div>
</body>


controller :



class Employee extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->model('EmployeeModel');
}

public function index(){
$search= $this->input->post('search');
$query = $this->EmployeeModel->getEmployee($search);
$this->load->view('index',$query);
echo json_encode ($query);

}


And the model:



class EmployeeModel extends CI_Model {

function __construct()
{
parent::__construct();
}
function getEmployee($search){
$this->db->select("EMPLOYEE_ID,FIRST_NAME,LAST_NAME");
$whereCondition = array('LAST_NAME' =>$search);
$this->db->where($whereCondition);
$this->db->from('trn_employee');
$query = $this->db->get();
return $query->result();
}
}




Aucun commentaire:

Enregistrer un commentaire