Reputation:
I've a problem with session on code igniter. First, i want to build an advance search function. The advance search function will allow user to search using several category. After the user search something and the result successfully return, on the left sidebar will show the search key and an "edit button". The edit button will allow user to go to the previous page and change their search key. That's why, i need to use session to store the search key. But after i set the session and try to retrieve the value, a problem has occurred.
This is the workflow. - Set the session on a.php - Get the session value within a function on b.php - Ajax to b.php to receive the session value and show it.
For information, i use php and code igniter for the server side.
This is the code:
a.php
if (!empty($params['modelname'])){
$modelid = "";
for($i = 0; $i < count($params['modelname']); $i++)
{
$modelid .= $params['modelname'][$i];
if($i+1 < count($params['modelname']))
{
$modelid .= ", ";
}
}
$data = array(
"modelname" => $modelid
);
$this->session->set_userdata($data);
?>
<span id="model"><strong><?php echo $modelid; ?><?php echo form_button('btnsearchmodel', 'Edit','class="btnsearchkata" id="btnsearchmodel"')?></strong></span>
<div class="clear"></div>
<?php }
b.php
public function getselectedmodel()
{
$data = $this->session->userdata('modelname');
echo $data;
}
c.php
function getCariModel()
{
var brandname = ""
$(".brand").each(function(){
if ($(this).is(':checked') ){
brandname += $(this).val()+',';
}
});
if(brandname != "")
{
$.ajax({
type: "POST",
url: '<?php echo site_url("ajax_data/getselectedmodel"); ?>',
dataType: 'json',
data: {c: "ambilmodel"},
success: function(data) {
alert(data);
}
});
}
}
The problem is: when I didn't receive the data. And when I try to alert the data, the alert didn't work at all. It seems like the request is not success
I guess the problem is when i set the session. but i really don't know what is wrong with my code. anyone could help me? Thank you
Upvotes: 1
Views: 264
Reputation: 10253
You are waiting JSON in javascript, but in your controller action getselectedmodel
you do not have json_encode. Try this:
public function getselectedmodel()
{
$data = $this->session->userdata('modelname');
$this->output->set_output(json_encode($data));
}
If you still have no output. Try going directly to action you are requesting and update your question.
Upvotes: 2