由于我刚接触CodeIgniter和Ajax,因此我无法正确使用CRUD。 ajax调用可以成功执行,但是不会验证应该对数据库进行的任何更改(将数据更新到数据库)。这是通过模式btw进行的ajax调用。在网络中,当我单击js文件时,它只是预览“ true”。
这是缩短的源代码
Manager_Model.php
public function managerList()
{
$this->db->order_by('m_ID', 'DESC');
$managerlist = $this->db->get('managers');
return $managerlist->result();
}
public function managerAdd()
{
$data = array(
'm_fname' => $this->input->post('fullname'),
'm_email' => $this->input->post('email'),
'm_contact' => $this->input->post('contact'),
'm_type' => $this->input->post('level'),
'm_dept' => $this->input->post('department'),
'm_username' => $this->input->post('username'),
'm_password' => $this->input->post('password'),
'm_status' => $this->input->post('status'),
'm_datecreated' => date('Y-m-d H:i:s')
);
$this->db->set('m_sessID', 'UUID()', FALSE);
$this->db->insert('managers', $data);
}
public function managerUpdate()
{
$emid = $this->input->post('emid');
$epassword = $this->input->post('epassword');
$efname = $this->input->post('efullname');
$estatus = $this->input->post('estatus');
$this->db->set('m_fname', $efname);
$this->db->set('m_password', $epassword);
$this->db->set('m_status', $estatus);
$this->db->where('m_ID', $emid);
$result = $this->db->update('managers');
return $result;
}
public function managerDelete()
{
$dmid = $this->input->post('dmid');
$this->db->where('m_ID', $dmid);
$result = $this->db->delete('managers');
return $result;
}
Admin.php(我的控制器)
public function __construct()
{
parent:: __construct();
$this->load->model('Manager_Model');
}
public function index()
{
$this->load->view('adminsales/admin');
}
public function managers()
{
$data = $this->Manager_Model->managerList();
echo json_encode($data);
}
public function makeManager()
{
$data = $this->Manager_Model->managerAdd();
echo json_encode($data);
}
public function updateManager()
{
$data = $this->Manager_Model->managerUpdate();
echo json_encode($data);
}
public function deleteManager()
{
$data = $this->Manager_Model->managerDelete();
echo json_encode($data);
}
我的Ajax文件
$(document).ready(function(){
show_manager(); //call function show all product
$('#mydata').dataTable();
//function show all manager
function show_manager(){
$.ajax({
type: 'ajax',
url: 'admin/managers',
async: true,
dataType: 'json',
success: function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html += '<tr>'+
'<td>'+data[i].m_ID+'</td>'+
'<td>'+data[i].m_username+'</td>'+
'<td>'+data[i].m_fname+'</td>'+
'<td>'+data[i].m_status+'</td>'+
'<td>'+data[i].m_lastlogintime+'</td>'+
'<td>'+data[i].m_lastloginIP+'</td>'+
'<td>'+data[i].m_loginsystem+'</td>'+
'<td style="text-align:right;">'+
'<a href="javascript:void(0);" class="btn btn-info btn-sm manager_edit" data-m_ID="'+data[i].m_ID+'" data-m_username="'+data[i].m_username+'" data-m_email="'+data[i].m_email+'" data-m_fname="'+data[i].m_fname+'" data-m_status="'+data[i].m_status+'">Edit'+data[i].m_ID+'</a>'+' '+
'<a href="javascript:void(0);" class="btn btn-danger btn-sm manager_delete" data-m_ID="'+data[i].m_ID+'">Delete</a>'+
'</td>'+
'</tr>';
}
$('#show_data').html(html);
}
});
}
//posting data to database
$('#btn_save').on('click',function(){
event.preventDefault();
var fullname = $('#fullname').val();
var email = $('#email').val();
var contact = $('#contact').val();
var department = $('#department').val();
var level = $('#level').val();
var status = $('#status').val();
var username = $('#username').val();
var password = $('#password').val();
var cpassword = $('#cpassword').val();
if (fullname == "" || email == "" || contact == "" || department == ""
|| level == "" || status == "" || username == "" || password == "") {
alert("Please input required fields!");
}
else if (password != cpassword) {
alert('Password did not matched!');
}
else {
$.ajax({
type : "POST",
url : "admin/makeManager",
dataType : "JSON",
data : {fullname:fullname, email:email, contact:contact, department:department,
level:level, status:status, username:username, password:password},
success: function(data){
$('[name="fullname"]').val("");
$('[name="email"]').val("");
$('[name="contact"]').val("");
$('[name="department"]').val("");
$('[name="level"]').val("");
$('[name="stats"]').val("");
$('[name="username"]').val("");
$('[name="password"]').val("");
alert('Data Added Successfully');
$('#insert_manager_form')[0].reset();
show_manager();
}
});
return false;
}
});
//get data for update record
$('#show_data').on('click','.manager_edit',function(){
var estatus = $(this).data('m_status');
var epassword = $(this).data('m_password');
var efullname = $(this).data('m_fname');
var eemail = $(this).data('m_email');
var eusername = $(this).data('m_username');
$('#Modal_Edit').modal('show');
$('[name="eusername"]').val(eusername);
$('[name="estatus"]').val(estatus);
$('[name="efullname"]').val(efullname);
$('[name="epassword"]').val(epassword);
$('[name="eemail"]').val(eemail);
});
//update record to database
$('#btn_update').on('click',function(){
var estatus = $('#estatus').val();
var epassword = $('#epassword').val();
var efullname = $('#efullname').val();
$.ajax({
type : "POST",
url : "admin/updateManager",
dataType : "JSON",
data : {estatus:estatus, epassword:epassword, efullname:efullname},
success: function(data){
$('[name="estatus"]').val("");
$('[name="epassword"]').val("");
$('[name="efullname"]').val("");
alert('Data Updated Successfully');
show_manager();
}
});
return false;
});
//get data for delete record
$('#show_data').on('click','.manager_delete',function(){
var dmid = $(this).data('m_ID');
$('#Modal_Delete').modal('show');
$('[name="dmid"]').val(dmid);
});
//delete record to database
$('#btn_delete').on('click',function(){
var dmid = $('#dmid').val();
$.ajax({
type : "POST",
url : "admin/deleteManager",
dataType : "JSON",
data : {dmid:dmid},
success: function(data){
$('[name="dmid"]').val("");
$('#Modal_Delete').modal('hide');
alert("Data has been deleted. It cannot be changed!");
show_product();
}
});
return false;
});
});
编辑模式
<form>
<div class="modal fade" id="Modal_Edit" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit Product</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<div class="col-md-10">
<input type="hidden" name="emid" id="emid" class="form-control" readonly>
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Username</label>
<div class="col-md-10">
<input type="text" name="eusername" id="eusername" class="form-control" readonly>
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Email</label>
<div class="col-md-10">
<input type="text" name="eemail" id="eemail" class="form-control" readonly>
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Full Name</label>
<div class="col-md-10">
<input type="text" name="efullname" id="efullname" class="form-control">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Status</label>
<div class="col-md-10">
<div class="radio">
<label><input type="radio" name="estatus" id="estatus" value="ON">ON</label>
<label><input type="radio" name="estatus" id="estatus" value="CLOSE">CLOSE</label>
</div>
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Password</label>
<div class="col-md-10">
<input type="password" name="epassword" id="epassword" class="form-control">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Confirm Password</label>
<div class="col-md-10">
<input type="password" name="ecpassword" id="ecpassword" class="form-control">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" type="submit" id="btn_update" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</div>
</form>
删除模态
<form>
<div class="modal fade" id="Modal_Delete" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Product</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<strong>Are you sure to delete this record?</strong>
</div>
<div class="modal-footer">
<input type="hidden" name="dmid" id="dmid" class="form-control">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
<button type="button" type="submit" id="btn_delete" class="btn btn-primary">Yes</button>
</div>
</div>
</div>
</div>
</form>
PS:添加和查看效果很好。我不知道我哪里错了。感谢您的回答。
答案 0 :(得分:0)
请在控制器中接收ID并将其发送给模型。
用下面的代码更改您的代码。
在控制器中
public function updateManager()
{
$emid = $this->input->post('emid');
$data = $this->Manager_Model->managerUpdate($emid);
echo json_encode($data);
}
public function deleteManager()
{
$data = $this->Manager_Model->managerDelete();
echo json_encode($data);
}
在模型中
public function managerUpdate($emid = '')
{
$epassword = $this->input->post('epassword');
$efname = $this->input->post('efullname');
$estatus = $this->input->post('estatus');
$this->db->set('m_fname', $efname);
$this->db->set('m_password', $epassword);
$this->db->set('m_status', $estatus);
$this->db->where('m_ID', $emid);
$result = $this->db->update('managers');
return $result;
}
public function managerDelete($dmid = '')
{
$this->db->where('m_ID', $dmid);
$result = $this->db->delete('managers');
return $result;
}
此外,请在将input
放在名称选择器之前尝试。
更改
$('[name="fullname"]').val("");
一个以下
$('input[name="fullname"]').val("");
答案 1 :(得分:0)
您还必须发送emid
控制器的updateManager()
:
//update record to database
$('#btn_update').on('click',function(){
var estatus = $('#estatus').val();
var epassword = $('#epassword').val();
var efullname = $('#efullname').val();
var emid = $('#emid').val();
$.ajax({
type : "POST",
url : "admin/updateManager",
dataType : "JSON",
data : {estatus:estatus, epassword:epassword, efullname:efullname, emid:emid},
success: function(data){
$('[name="estatus"]').val("");
$('[name="epassword"]').val("");
$('[name="efullname"]').val("");
alert('Data Updated Successfully');
show_manager();
}
});
return false;
});