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;
}
The 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 controller when i print the search_data that is print_r($search_data); die(); i did not get any result.
I think ajax call is not get in to the controller Please provide solution for this
Moreover, when I echo out the query in controller that is print_r($query) ; I did not get any result. Also autosearch is not working. Please provide solution for this.
I think it is ajaxSearch function not runing.
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 you
You need to pass all ur data into array and then convert it into json_encode for more referrence about encoding -> http://php.net/manual/en/function.json-encode.php..
here is 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 you 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 look at you're browser console panel. and see the retrieved data in your function and also try to check if your ajax url is correct.
Related
i have to get the html reply of two php function with AJAX
Here my code
Home.php
<script>
$( document ).ready(function(){
var parameter = {
"mynumber" : $('#mynumber').val()
};
$.ajax({
data: parameter ,
url: 'script.php',
type: 'post',
dataType: 'json',
beforeSend: function () {
$("#loading").show();
},
success: function (response) {
$("#loading").hide();
$("#div1").html(response.reply1);
$("#div2").html(response.reply2);
},
}); });
</script>
And script.php
function loopone(){
for($a=0;$a<10;$a++){
?><div id="mydiv"><?php echo $a;?></div>
}
}
function casetwo(){
if($a<>$g){
?><div id="mydiv2"><?php echo $a;?></div>
}
}
$prew1=file_get_contents(loopone());
$prew2=file_get_contents(casetwo());
$reply1=prew1;
$reply2=prew2;
echo json_encode(array("reply1"=>$reply1, "reply2"=>$reply2));
what's wrong here ? I can not see the results.
file_get_contents() is for reading a file or URL into a string. You don't need to use these if you're creating the content in your script. Just have your functions return strings.
function loopone() {
$result = "";
for (a = 0; $a < 10; $a++) {
$result .= "<div class='mydiv'>$a</div>";
}
return $result;
}
function casetwo() {
global $a, $g;
if ($a != $g) {
return "<div id='mydiv2'>$a</div>";
} else {
return "";
}
}
$prew1 = loopone();
$prew2 = casetwo();
echo json_encode(array("reply1"=>$prew1, "reply2"=>$prew2));
I changed id="mydiv" to class="mydiv" because IDs are supposed to be unique, you shouldn't return the same ID in a loop.
AJAX Autosearch
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function ajaxSearch() {
alert('hai');
var input_data = $('#search_data').val();
alert(input_data);
$.ajax({
type: "POST",
url: "search/auto_search",
data: {search_data:input_data},
success: function(data1) {
alert(data1);
// return success
if (data1.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data1);
}
}
});
}
</script>
CONTROLLER
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;
}
MODEL
public function autocomplete($search_data) {
$this->db->select('name');
$this->db->select('uid');
$this->db->like('name', $search_data);
$dt = $this->db->get('tbl_reg', 10);
//print_r($dt); die();
return $dt->result();
}
When I echo out the search_data in controller I got the whole HTML page whats the reason for that and would you please provide solution for this problem?
change in ajax call as well.
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function ajaxSearch() {
alert('hai');
var input_data = $('#search_data').val();
alert(input_data);
$.ajax({
type: "POST",
url: "/search/auto_search",
data: {search_data:input_data},
success: function(data1) {
alert(data1);
// return success
if (data1.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data1);
}
}
});
}
change controller as following.
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();
$str='';
foreach ($query as $row):
$str.=$row->uid ;
$str.=$row->name ;
endforeach;
echo $str;
}
this will work as you are already return result in model and you do not need to use $query->result();
I want to get data from database but I cannot pass variable from view to controller using json. Could you please help me to find the mistake.
Here is my view:
<?php
echo $message;
foreach($results as $row)
{
$faq_id = $row->faq_id;
$faq_title = $row->faq_title;
?>
<h3><?php echo $faq_title; ?></h3>
<?php
}
?>
Here is my JS file:
$(".faq_title").click(function(){
var title = $(this).text();
$.ajax({
url: 'faq/get_faq_data',
data: {'title': title},
dataType: 'json',
type: "post",
success: function(data) {
response = jQuery.parseJSON(data);
console.log(response);
}
});
});
Here is my Controller:
public function get_faq_data() {
header('Content-Type: application/json',true);
$this->load->model("model_faq");
$title = $this->input->post('title');
$data["results"] = $this->model_faq->did_get_faq_data($title);
echo json_encode($data["results"]);
}
Here is my model:
public function did_get_faq_data($title){
$this->db->select('*');
$this->db->from('faq');
$this->db->where('faq_title', $title);
$query = $this->db->get('faq');
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
var title = $(this).text();
$.post( // you can simply send json from post method of jquery
"<?php echo site_url('faq/get_faq_data') ?>", // try to use full url
{
title : title
},
function( data ) {
console.log( data );
},
"json"
);
In the below codeigniter code i have placed controller,model and view.My aim when i select exam name it should drop corresponding course code from examcourse table.But my actual result when i select exam name it is not droping the dropdown .Pls help me to do this.
Controller:import
public function index()
{
//echo "inside form upload";
$data = array();
//$college_name = $this->session->userdata('college_name');
if($query = $this->import_model->get_exam_data())
{
$data['exam_data'] = $query;
}
//$this->load->view('student_view', $data);
$this->load->view('form_upload',$data);
}
function subjectcodedetails()
{
$data = array();
//$college_name = $this->session->userdata('college_name');
$exam_name = $this->input->post('exam_name');
//$course_name = $this->input->post('course_name');
if($query = $this->import_model->get_subject_records($exam_name))
{
$data['course_code'] = $query;
}
$this->load->view('subject_view', $data);
}
model:
function get_exam_data()
{
$this->db->distinct();
$this->db->select("CONCAT(exam_name) AS fullexamname", FALSE);//this will concat the value
//$this->db->where('college_name',$college_name);
$query = $this->db->get('examcourse');
return $query->result();
}
function get_subject_records($exam_name)
{
//echo "exam_name inside get_subject_records".$exam_name;
$this->db->select('course_code');
//$this->db->where('exam_name',$exam_name);
$this->db->where('fullexamname',$exam_name);
//$this->db->where('college_name',$college_name);
$query = $this->db->get('examcourse');
return $query->result();
}
view:
form_upload
<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(){
$(".dropdown_class").change(function()
{
var id=$(this).val();
// Please find the course_code, course_code was not found
var dataString = 'course_code='+ id;
$.ajax
({
type: "POST",
url: "import/subjectcodedetails",
data: dataString,
cache: false,
success: function(html)
{
$(".dropdown_class12").html(html);
}
});
});
</script>
<form action="http://localhost/CodeIgniter/index.php/import/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data"> <?php
$data = array();
$data["Select Exam Name"] = "Select Exam Name";
foreach ($exam_data as $row)
{
$data[$row->fullexamname] = $row->fullexamname;
}
echo form_dropdown('exam_name', $data, 'small', 'class="dropdown_class" id="exam_name_id" ');
?>
view:subject_view
<?php
$data = array();
foreach ($course_records as $row)
{
$data[$row->course_code] = $row->course_code;
}
echo form_dropdown('course_name', $data, 'small','class="dropdown_class12" id="course_name_id"');
?>
$(document).ready(function () {
$(".dropdown_class").change(function () {
var id = $(this).val();
// Please find the course_code, course_code was not found
var dataString = 'course_code=' + id;
$.ajax({
type: "POST",
url: "import/subjectcodedetails",
data: dataString,
cache: false,
success: function (html) {
$(".dropdown_class12").html(html);
}
});
});
};
Missing } at the end of $(document).ready block
I know this may be the [insertLongNumber]th time someone's ask this question, I did my research but I can't find another answer that fits my problem. So here it is.
I'm working on a dynamic dropdown with php and ajax, in codeigniter. I'm new to CI and I have a basic knowledge of Ajax.
What I have noticed so far is that in the console, it is not recognizing the value coming from the first dropdown, so i get departamento_id : undefined
This makes me thing problem comes from ajax script (i got it of the web)
My view, with the ajax code included
<?php
$this->load->helper('html');
?>
<html>
<head>
<title>Buscador</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#dpto-dropdown select').change(function () {
var selDpto = $(this).attr('value');
console.log(selDpto);
$.ajax({
url: "test/ajax_call",
async: false,
type: "POST",
data: "departamento_id="+selDpto,
dataType: "html",
success: function(data) {
$('#ciudad').html(data);
}
})
});
});
</script>
</head>
<?php echo form_open('test/buscar');?>
<?php
<div id='dpto-dropdown'><?php print form_dropdown('departamentos', $departamento) ?></div>
<div id="ciudad"><select><option value=''>-</option></select></div>
//rest of code...
This is my Controller code:
class Test extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('buscador_model');
}
function index()
{
$departamentos = $this->buscador_model->traerInfoDptos();
$precios = $this->buscador_model->traerPrecioHoteles();
foreach($departamentos as $departamento){
$dpto_final[$departamento->id] = $departamento->nom_departamento;
}
$info = array(
'departamento' => $dpto_final,
'precios' => $precios,
);
$this->load->view('buscador_view', $info);
}
function ajax_call()
{
//check to see people wont go directly
if (isset($_POST) && isset($_POST['departamento_id']))
{
$dpto = $_POST['departamento_id'];
$ciudad = $this->buscador_model->traerCiudadPorDpto($dpto);
foreach ($ciudad as $c)
{
$ciudadfinal[$c->cod_ciudad] = $c->nom_ciudad;
}
//dropdown
echo form_dropdown('Ciudades', $ciudadfinal);
}
else
{
redirect('index');
}
}
}
this is my model:
Class Buscador_model extends CI_Model
{
function traerInfoDptos()
{
$this->db->select('id, nom_departamento');
$this->db->from('departamento');
$query = $this->db->get();
if ($query->num_rows > 0)
{
return $query->result();
}
}
function traerCiudadPorDpto($dpto)
{
$query = $this->db->query("SELECT nom_ciudad, cod_ciudad FROM ciudad WHERE departamento_id = '{$dpto}'");
if ($query->num_rows > 0)
{
return $query->result();
}
}
}// end buscador model class
See this page: http://www.onerutter.com/open-source/jquery/jquery-tips-how-to-get-value-of-selected-option-in-select-box.html
You need to use .val() instead of .attr('value')
<script type="text/javascript">
$(document).ready(function () {
$('#dpto-dropdown select').change(function () {
var selDpto = $(this).val(); // <-- change this line
console.log(selDpto);
$.ajax({
url: "test/ajax_call",
async: false,
type: "POST",
data: "departamento_id="+selDpto,
dataType: "html",
success: function(data) {
$('#ciudad').html(data);
}
})
});
});
</script>