Habib Rehman Habib Rehman - 6 months ago 18
Ajax Question

Codeigniter: Ajax data is printing in log but not working in function

I'm getting data through ajax who's function is:


<script type="text/javascript">
// Ajax post
$(document).ready(function()
{

$("#submit").click(function(event)
{
event.preventDefault();
var hiddenValue = $("#hiddenValue").val();

alert(hiddenValue);

var update_name = $("input#update_name").val();

// pop up Name Entered
alert(update_name);

jQuery.ajax(
{
type: "POST",
url: "<?php echo base_url(); ?>" + "seasons/update_season",

data: {
hiddenValue : hiddenValue,
update_name: update_name
},

success: function(res)
{
console.log(res);
// window.alert("i got some data ");
if (res)
{
jQuery("div#result").show();
}
},
fail: function(res)
{
console.log(res);
}
});
});
});



The Controller function i have:


public function update_season()
{
$session_id = $this->session->userdata('id');
if (isset($session_id))
{

// print_r($_POST);
// die();
$update_id = $this->input->post('hiddenValue');
$update_name = $this->input->post('update_name');

$arr = array(
'id' => $update_id,
'name'=> $update_name);

//This prints empty data
// print_r($arr);
// die();

$result = $this->model_season->edit_season($arr);
// $result = $result->row();

if ($result)
{
print_r($arr);
}

else
{
return FALSE;
}

}
else
{
redirect('user_authentication');
}
}


And in Model through controller i have:


public function edit_season($data)
{
// I am getting right array of name and id
print_r($data);
die();

// but get empty variable if i try to assign value to it
$name = $data['name'];

$this->db->where('seasons', array('season_id ' => $data['id']));
$query = $this->db->update('seasons',array('names ' => $data['name'] ));

if ($query)
{
return $query;
}
else
{
return FALSE;
}

}


The ajax seem to work fine as its printing the values of
id
and
name
its getting i'm not even encoding it in json, but i'm unable to get its value in separate variable. I wonder if there is any different method to get values from ajax data ?

When i let it run the whole model function without making it die i have following error:

UPDATE
seasons
SET
names
= NULL WHERE
seasons
=
Array``
Like array have nothing in it

Answer

There is error in your query, you are supplying array to where condition, where it should be string,

$this->db->where('season_id ', $data['id']);

Also, it is not good to have unnecessary spaces (though CI driver internally trims all spaces) in conditions like 'season_id ' should be 'season_id'

$this->db->where('season_id', $data['id']);
$query = $this->db->update('seasons', array('names' => $data['name']));

Check driver referance here: Queries in CI