Ajax auto search in codec did not work
Controller code:
public function auto_search() {
$search_data = $this->input->post('search_data');
//print_r($search_data); die();
$query = $this->search_model->autocomplete($search_data);
print_r($query); die();
foreach ($query->result() as $row):
echo $row->uid ;
echo $row->name ;
endforeach;
}
javascript:
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function ajaxSearch() {
var input_data = $('#search_data').val();
$.ajax({
type: "POST",
url: "/search/auto_search",
data: {search_data:input_data},
success: function(data1) {
alert(data1);
if (data1.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data1);
}
}
});
}
</script>
In the controller, when I print the search_data that print_r($search_data); die();
, I got no result. I think the ajax call is not in the controller. Please provide a solution for this
Also, when I repeat the request in the controller which print_r($query) ;
, I got no result. Also, automatic search does not work. Please provide a solution for this.
source to share
I think the ajaxSearch function is not getting triggered. your try
public function auto_search()
{
$search_data = $this->input->post('search_data');
$query = $this->search_model->autocomplete($search_data);
if (!empty($query)) {
foreach ($query->result() as $row) {
echo $row->uid;
echo $row->name;
}
} else {
echo '';
}
}
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$(function () {
function ajaxSearch() {
var input_data = $('#search_data').val();
$.ajax({
type: "POST",
url: "/search/auto_search",
data: {search_data: input_data},
success: function (data1) {
alert(data1);
if (data1.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data1);
}
}
});
}
// start ajaxSearch
ajaxSearch();
});
</script>
thank
source to share
You need to pass all ur data into an array and then convert it to json_encode for more information on encoding -> http://php.net/manual/en/function.json-encode.php ..
here is some sample code for you.
public function auto_search() {
$search_data = $this->input->post('search_data');
//print_r($search_data); die();
$query = $this->search_model->autocomplete($search_data);
//print_r($query); die(); <- you should replace the die.
$myArray = [];
foreach ($query->result() as $key => $row):
$myArray['keyname'] = $row->uid;
$myArray['keyname1'] = $row->name;
endforeach;
//data retrieves by jquery
echo json_encode($myArray);
}
In your jquery.
$.ajax({
type: "POST",
url: "/search/auto_search",
data: {search_data:input_data},
success: function(data1) {
console.log(data1);
console.log(data1.keyname);
console.log(data1.keyname1);
if (data1.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data1);
}
}
});
try to see you are browser console bar. and see the received data in your function and also try to validate your ajax url.
source to share