I am just start creating a database update system with a bootstrap modal window. I am using ajax to populate the modal form fields but not getting any value. Please review the code and help me to figure out the error.
<!-- Modal -->
<div class="modal fade" id="edit_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Edit Data</h4>
</div>
<div class="modal-body">
<div class="content-wrapper content-wrapper--with-bg">
<?php echo form_open('quotation'); ?>
<p>
<input type="text" name="bname" id="bname" required class="form-control custom-ctrl" placeholder="Billing Name">
</p>
<span class="text-danger"><?php echo form_error('bname'); ?></span>
<p>
<input type="text" name="company" id="company" required class="form-control custom-ctrl" placeholder="Company">
</p>
<span class="text-danger"><?php echo form_error('company'); ?></span>
<input type="hidden" name="id" id="id">
</p>
<button type="submit" id="update" class="btn btn-primary btn-custom pull-right"> Update</button>
<br>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
My Controller Function
public function fetch_by_id(){
$this->load->model("quotation_model");
$result = $this->quotation_model->fetch_by_id();
echo json_encode($result);
}
And Here is the Modal associated with the controller.
public function fetch_by_id(){
$id = $this->input->get('id');
$this->db->where('id', $id);
$query = $this->db->get('quotes');
return $query->result();
}
Here is the ajax code
$(document).on('click', '.editBtn', function(){
var id = $(this).attr('data');
$('#edit_modal').modal('show');
$.ajax({
type: 'ajax',
method: 'get',
url: '<?php echo base_url(); ?>quotation/fetch_by_id',
data: {id: id},
async: false,
dataType: 'json',
success: function(data){
$("input[name=id]").val(data.id);
$("input[name=bname]").val(data.bname);
$("input[name=company]").val(data.company);
},
error: function(){
alert('Could not displaying data');
},
});
});
I am just able to popup the modal window but not getting any value