I have created a function that adds a row after confirmation. The issue is that after submitting, the tables do not reload and show an error alert. In reality, the data is successfully saved but I need to refresh the page for the table to reload. Below is my Ajax jQuery code:
function reloadPage()
{
window.location.reload();
}
function save()
{
$('#btnSave').text('saving...');
$('#btnSave').attr('disabled', true);
var url;
if(save_method == 'add') {
url = "<?php echo site_url('activity/save')?>";
} else {
url = "<?php echo site_url('activity/update_activity')?>";
}
$.ajax({
url : url,
type: "POST",
data: $('#form-input').serialize(),
dataType: "JSON",
success: function(data)
{
$('#myModal').modal('hide');
reloadPage();
$('#btnSave').text('save');
$('#btnSave').attr('disabled', false);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
$('#btnSave').text('save');
$('#btnSave').attr('disabled', false);
}
});
}
<button id="btnSave" onclick="save()" class="btn green">Save</button>
My controller:
public function save() {
$actype = $this->input->post('actype');
$activity_name = $this->input->post('activity_name');
$project = $this->input->post('project');
$portion = $this->input->post('portion');
$activity = $this->input->post('actid');
$data = array(
'activity_type_id' => $actype,
'activity_name' => $activity_name,
'project_id' => $project,
'portion' => $portion,
'activity_id' => $activity
);
$this->activity->insertactivity($data);
redirect("activity/input");
}
After clicking the save button, it shows an 'Error adding / update data' alert, however, the data has actually been saved when the page is reloaded. Where is the error in my Ajax code?