Habib Rehman Habib Rehman - 6 months ago 10
jQuery Question

Codeigniter: Ajax sending id & name but function is receiving only id

I'm getting the id but not the name, what's wrong i'm doing ?
Please take a look at my CI code and ajax function below:

This is View on which i'm calling ajax:


<div class="modal fade" id="myModal<?php echo $values->season_id; ?>"
tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Update Seasons</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input type="hidden" id="hiddenValue" name="hiddenValue" value="<?php echo $values->season_id; ?>">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Season Name:
</label>
<div class="title_right">
<div class="input-group">
<input type="text" class="form-control" name="update_name" id="update_name" value="<?php echo $values->names; ?>">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" id="submit" value="Save changes">
</div>
</div>
</div>
</div>


This is Ajax Function:


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

alert("Ajax function started working");

$("#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",
dataType: 'json',

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

success: function(res)
{
console.log(res);
// window.alert("i got some data ");
if (res)
{
alert("changes made");
}
}
});
});
});




And here i'm trying to capture the value:


public function update_season()
{
$session_id = $this->session->userdata('id');
if (isset($session_id))
{
$update_id = $this->input->post('hiddenValue');
$update_name = $this->input->post('update_name');

$result = $this->model_season->update_season($update_id,$update_name);

if ($query)
{
$query = $query->row();
$data = array(
'season_id' => $query->season_id,
'names' =>$query->names
);
echo json_encode($data);
}
else
{
return FALSE;
}

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

Answer

You use

 if ($query) 
    {
        $query = $query->row();
        $data  = array(
            'season_id' => $query->season_id,
            'names' =>$query->names 
            );
        echo json_encode($data);
    }

but where your $query. As your code It should have as following

if ($result) 
        {
            $data  = array(
                'season_id' => $result->season_id,
                'names' =>$result->names 
                );
            echo json_encode($data);
        }

Now use $result->season_id instead of $query->season_id and make sure you use row() method in your model update_season() for returning desired values