如何在 php codeigniter 中添加来自 CRUD ajax 的数据?
How to ADD data from CRUD ajax in php codeigniter?
我想在下面的table中添加更新和删除数据。删除正在工作。但是我在使用 CRUD 添加数据时遇到问题
这里是控制器。我将数据添加到两个连接 tables、parent 和学生中,只需单击一个按钮即可使用单个表单。
public function register_students()
{
// $this->load->model('Register_model','multi_model',TRUE);
$encrypted_password1 = $this->encrypt->encode($this->input->post('p_pwd'));
$parent_data = array(
'parent_code' => $this->input->post('parent_code'),
'f_name' => $this->input->post('p_first_name'),
'l_name' => $this->input->post('p_last_name'),
'DOB' => $this->input->post('p_dob'),
'address' => $this->input->post('p_address'),
'tel' => $this->input->post('p_tel_no'),
'email' => $this->input->post('email'),
'username' => $this->input->post('p_username'),
'password' => $encrypted_password1,
);
$id = $this->Model_Action->insertTable('parent',$parent_data);
$encrypted_password = $this->encrypt->encode($this->input->post('pwd'));
$student_data = array(
's_id' => '',
'student_code' => $this->input->post('student_code'),
'f_name' => $this->input->post('first_name'),
'l_name' => $this->input->post('last_name'),
'DOB' => $this->input->post('dob'),
'gender' => $this->input->post('gender'),
'address' => $this->input->post('address'),
'tel' => $this->input->post('tel_no'),
'username' => $this->input->post('username'),
'password' => $encrypted_password,
'p_id' => $id
);
$insert = $this->Model_Action->insertTable('student',$student_data);
echo json_encode(array("status" => TRUE));
redirect('student');
}
这是我的模型
function insertTable($table, $data) {
$this->db->insert($table, $data);
return $this->db->insert_id();
}
这是我的观点 - javascript 部分。我删除了这部分的编辑功能和删除功能。
<script type="text/javascript">
$(document).ready( function () {
$('#student_table').DataTable();
} );
var save_method;
var table;
function add_book()
{
save_method = 'add';
$('#form')[0].reset();
$('#modal_form').modal('show');
}
function save()
{
var url;
if(save_method == 'add')
{
url = "<?php echo site_url('/Student/register_students')?>";
}
else
{
url = "<?php echo site_url('/Student/student_update')?>";
}
// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data)
{
//if success close modal and reload ajax table
$('#modal_form').modal('hide');
location.reload();// for reload a page
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
我使用 bootstrap 模型来添加和更新表单,这些模型也工作正常。
据我所知,您在 ajax 调用中使用了 redirect('student');。如果您想使用 ajax,请点做。我劝你:
$array = array(
'link' => 'strudent',
'status' => true
);
echo json_encode($array);
return;
然后 ajax 成功:
window.location.href = data.link;
我想在下面的table中添加更新和删除数据。删除正在工作。但是我在使用 CRUD 添加数据时遇到问题
这里是控制器。我将数据添加到两个连接 tables、parent 和学生中,只需单击一个按钮即可使用单个表单。
public function register_students()
{
// $this->load->model('Register_model','multi_model',TRUE);
$encrypted_password1 = $this->encrypt->encode($this->input->post('p_pwd'));
$parent_data = array(
'parent_code' => $this->input->post('parent_code'),
'f_name' => $this->input->post('p_first_name'),
'l_name' => $this->input->post('p_last_name'),
'DOB' => $this->input->post('p_dob'),
'address' => $this->input->post('p_address'),
'tel' => $this->input->post('p_tel_no'),
'email' => $this->input->post('email'),
'username' => $this->input->post('p_username'),
'password' => $encrypted_password1,
);
$id = $this->Model_Action->insertTable('parent',$parent_data);
$encrypted_password = $this->encrypt->encode($this->input->post('pwd'));
$student_data = array(
's_id' => '',
'student_code' => $this->input->post('student_code'),
'f_name' => $this->input->post('first_name'),
'l_name' => $this->input->post('last_name'),
'DOB' => $this->input->post('dob'),
'gender' => $this->input->post('gender'),
'address' => $this->input->post('address'),
'tel' => $this->input->post('tel_no'),
'username' => $this->input->post('username'),
'password' => $encrypted_password,
'p_id' => $id
);
$insert = $this->Model_Action->insertTable('student',$student_data);
echo json_encode(array("status" => TRUE));
redirect('student');
}
这是我的模型
function insertTable($table, $data) {
$this->db->insert($table, $data);
return $this->db->insert_id();
}
这是我的观点 - javascript 部分。我删除了这部分的编辑功能和删除功能。
<script type="text/javascript">
$(document).ready( function () {
$('#student_table').DataTable();
} );
var save_method;
var table;
function add_book()
{
save_method = 'add';
$('#form')[0].reset();
$('#modal_form').modal('show');
}
function save()
{
var url;
if(save_method == 'add')
{
url = "<?php echo site_url('/Student/register_students')?>";
}
else
{
url = "<?php echo site_url('/Student/student_update')?>";
}
// ajax adding data to database
$.ajax({
url : url,
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data)
{
//if success close modal and reload ajax table
$('#modal_form').modal('hide');
location.reload();// for reload a page
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
}
});
}
我使用 bootstrap 模型来添加和更新表单,这些模型也工作正常。
据我所知,您在 ajax 调用中使用了 redirect('student');。如果您想使用 ajax,请点做。我劝你:
$array = array(
'link' => 'strudent',
'status' => true
);
echo json_encode($array);
return;
然后 ajax 成功:
window.location.href = data.link;