我试图在CodeIgniter中创建一个只有不超过5个字段的单页CRUD。我的问题是它没有将数据插入数据库,我一直在调试这个,但我无法弄清楚问题是什么。我错过了什么?
形式:
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="activity-item">
<form id="agencyForm" enctype="multipart/form-data" method="POST" class="form-horizontal">
<div class="form-body">
<input type="hidden" value="" name="agency_id" />
<div class="form-group">
<div class="col-md-12">
<input name="agency_name" id="agency_name" placeholder="Agency Name" class="form-control" type="text">
<?php echo form_error('agency_name','<span class="help-block">','</span>'); ?>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="category" id="category" placeholder="Category" class="form-control" type="text">
<?php echo form_error('category','<span class="help-block">','</span>'); ?>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="address" id="address" placeholder="Address" class="form-control" type="text">
<?php echo form_error('address','<span class="help-block">','</span>'); ?>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input name="acronym" id="acronym" placeholder="Acronym" class="form-control" type="text">
<?php echo form_error('acronym','<span class="help-block">','</span>'); ?>
</div>
</div>
</div>
<button type="submit" value="submit " id="btnSave" onclick="save(this.agencyForm); return false" class="btn btn-effect">Save</button>
</form>
</div>
<div class="clear"> </div>
脚本:
<script type="text/javascript">
var save_method; //for save method string
var table;
$(document).ready(function() {
showAllAgency();
//datatables
table = $('#').DataTable({
"processing": true,
"serverSide": true,
"order": [],
"ajax": {
"url": "",
/*<?php echo site_url('agency/list')?>*/
"type": "POST"
},
//Set column definition initialisation properties.
"columnDefs": [{
"targets": [-1], //last column
"orderable": false, //set not orderable
}, ],
});
//set input/textarea/select event when change value, remove class error and remove text help block
$("input").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
$("textarea").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
$("select").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
});
function add_person() {
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show');
$('.modal-title').text('Add Agency');
}
function edit_person(id) {
save_method = 'update';
$('#form')[0].reset();
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
//Ajax Load data from ajax
$.ajax({
url: "<?php echo site_url('agency/edit_agency/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data) {
$('[name="id"]').val(data.id);
$('[name="agency_name"]').val(data.agency_name);
$('[name="category"]').val(data.category);
$('[name="address"]').val(data.address);
$('[name="acronym"]').val(data.acronym);
$('#modal_form').modal('show');
$('.modal-title').text('Edit Agency');
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error get data from ajax');
}
});
}
function reload_table() {
table.ajax.reload(null, false);
}
function save() {
$('#btnSave').text('saving...');
$('#btnSave').attr('disabled', true);
var url;
if (save_method == 'add') {
url = "<?php echo site_url('agency/save_c')?>";
} else {
url = "<?php echo site_url('agency/update_c')?>";
}
console.log($('#agencyForm').serialize());
// alert($(agencyForm).serialize());
$.ajax({
url: url,
// url: "<?php echo site_url('agency/save_c')?>",
type: "POST",
data: $('#agencyForm').serialize(),
dataType: "JSON",
success: function(data) {
if (data.status) {
alert('Successfully added the officer');
reload_table();
} else {
for (var i = 0; i < data.inputerror.length; i++) {
$('[name="' + data.inputerror[i] + '"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="' + data.inputerror[i] + '"]').next().text(data.error_string[i]); //select span help-block class set text error string
}
}
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error adding / update data');
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
}
});
}
function showAllAgency() {
$.ajax({
type: 'ajax',
url: '<?php echo base_url() ?>agency/getAgency',
async: false,
dataType: 'json',
success: function(data) {
var html = '';
var i;
for (i = 0; i < data.length; i++) {
html += '<tr>' +
'<td>' + data[i].agency_name + '</td>' +
'<td>' + data[i].category + '</td>' +
'<td>' + data[i].address + '</td>' +
'<td>' + data[i].acronym + '</td>' +
// '<td>'+
// '<a href="javascript:;" class="btn btn-info item-edit" data="'+data[i].agency_id+'">Edit</a>'+
// '<a href="javascript:;" class="btn btn-danger item-delete" data="'+data[i].agency_id+'">Delete</a>'+
// '</td>'+
'</tr>';
}
$('#showagency').html(html);
},
error: function() {
alert('Could not get Data from Database');
}
});
}
</script>
控制器:
public function save_c() {
var_dump($this - > input - > post(NULL, TRUE));
$data = array(
'agency_name' => $this - > input - > post('agency_name'),
'category' => $this - > input - > post('category'),
'address' => $this - > input - > post('address'),
'acronym' => $this - > input - > post('acronym'),
);
$insert = $this - > agency - > save_now($data);
echo json_encode(array("status" => TRUE));
}
型号:
public function save_now($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
注意:当我更改URL时,它会将数据插入数据库:
$.ajax({
// url: url,
url: "<?php echo site_url('agency/save_c')?>",
答案 0 :(得分:0)
你这里有问题。您尚未在任何地方指定save_method
。根据您的逻辑定义save_method
或删除此if条件并直接指定插入URL
if (save_method == 'add') {
url = "<?php echo site_url('agency/save_c')?>";
} else {
url = "<?php echo site_url('agency/update')?>";
}
您可以直接插入
url = "<?php echo site_url('agency/save_c')?>";