我尝试使用AJAX更新我的数据库
这是我的ajax功能
$(document).ready(function() {
$('#submit').click(function (event) {
event.preventDefault();
if($('#category').val()===''){
alert('Please enter a category name.')
}
else{
var category = $('#category').val();
var subcategory = $('#subcategory').val();
alert(category);
alert(subcategory);
$.ajax({
type: "POST",
url: "Admin_controller/insertCategory",
data:{ category:category, subcategory:subcategory },
success: function(data) {
alert('You can now add items.');
}
});
}
});
});
这是我的Admin_controller
public function insertCategory(){
$category = $this->input->post('category');
$subcategory = $this->input->post('subcategory');
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('ADMIN_ADDCategory');
}
else{
$category_id = $this->admin_model->insertCategory($category);
$this->admin_model->insertSubcategory($category_id, $subcategory);
}
}
这是我的模特
function insertCategory($category){
$data = array(
'category' => $category,
'status' => 1
);
$this->db->insert('category', $data);
$this->db->insert('category', $data);
$id = $this->db->insert_id(): //assign the last inserted id to variable $id
return $id;
}
function insertSubcategory($category_id, $subcategory){
$data = array(
'category_id' => $category_id,
'subcategory' => $subcategory,
'status' => 1
);
$this->db->insert('subcategory', $data);
}
当我单击应该触发AJAX功能的按钮时,它会显示alert('You can now add items.');
,但它不会使用我所需的值更新我的数据库。
我已经尝试过了
var category = $('#category').val();
var subcategory = $('#subcategory').val();
alert(category);
alert(subcategory);
查看是否获得我的输入,是的。警报后它只刷新我的页面。
我做错了什么?谢谢你的帮助。
答案 0 :(得分:0)
<强>更改强>
在Ajax中
$(document).ready(function() {
$('#submit').click(function(event) {
event.preventDefault();
if($('#category').val()==='' AND $('#subcategory').val() === ''){
alert('Fields should not be empty')
}
else{
var category = $('#category').val();
var subcategory = $('#subcategory').val();
$.ajax(
{
type:"post",
url: "<?php echo base_url(); ?>index.php/Admin_controller/insertCategory",
data:{ category:category, subcategory:subcategory},
success:function(data)
{
alert('You can now add items.');
}
}
);
}
});
});
在模型中
function insertCategory($category){
$data = array(
'category' => $category,
'status' => 1
);
$this->db->insert('category', $data);
$id = $this->db->insert_id(); # get last insert ID
return $id
}
function insertSubcategory($category_id, $subcategory){
$data = array(
'category_id' => $category_id,
'subcategory' => $subcategory,
'status' => 1
);
$this->db->insert('subcategory', $data);
}
<强> Admin_controller 强>
public function insertCategory(){
$category = $this->input->post('category');
$subcategory = $this->input->post('subcategory');
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('ADMIN_ADDCategory');
}
else{
$category_id = $this->admin_model->insertCategory($category);
$this->admin_model->insertSubcategory($category_id, $subcategory);
}
}
答案 1 :(得分:0)
您的ajax请求没有要发送的任何POST数据。是的,它正在向控制器发出POST请求,但没有数据被发送。 在这种情况下,Admin控制器中的类别和子类别变量将为false,从而导致问题。
您需要在AJAX请求中将类别和子类别作为数据发送:
在JS代码中,如果子类别存在与否,则尚未进行验证。
此外,在发出AJAX请求之前,您需要声明两个变量category和subcategory。这就是AJAX请求的样子(我没有做任何验证)。
var category = $('#category').val();
var subcategory = $('#subcategory').val();
$.ajax({
type: "POST",
data:{ category:category, subcategory:subcategory},
url: "<?=base_url()?>index.php/Admin_controller/insertCategory",
success: function(data) {
alert('You can now add items.');
}
});
除此之外,您还可以通过在模型中更改此代码来优化代码:
$this->db->insert('category', $data);
$this->db->select('id');
$this->db->from('category');
$this->db->where('category', $category);
$this->db->limit(1);
$query = $this->db->get();
return $query->result();
进入这个:
$this->db->insert('category', $data);
return $this->db->insert_id();
如果有任何疑问,请告诉我。