红酒泡绿茶 2016-02-01 14:09
浏览 70
已采纳

使用AJAX将项目插入数据库

I'm trying to update my database using AJAX

This is my ajax function

$(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.');
            }
        });
    }
  });
});

Here is my 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);
    }
}

Here is my model

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);
}

When I click the button that is supposed to trigger the AJAX function it shows the alert('You can now add items.'); but it doesn't update my database with my required values.

I already tried doing

var category = $('#category').val();
var subcategory = $('#subcategory').val();

alert(category);
alert(subcategory);

to view if it gets my inputs and yes it does. After the alert it just refreshes my page.

What am i doing wrong? Thanks for the help.

  • 写回答

2条回答 默认 最新

  • douaoli5328 2016-02-01 15:37
    关注

    Your ajax request is not having any POST data to send. Yes it is making a POST request to the controller, but no data is being sent. The category and subcategory variables in Admin controller will be false in that case, resulting in the issue.

    You need to send category and subcategory as data in your AJAX request:

    In your JS code, you haven't done validation if subcategory is there or not.

    Also, before making the AJAX request, you will need to declare two variables category and subcategory. This is how the AJAX request will look like (I haven't done any validations).

    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.');
        }
    });
    

    Apart from that, you can also optimise your code by changing this in your model:

        $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();
    

    into this:

        $this->db->insert('category', $data);
    
        return $this->db->insert_id();
    

    If any doubts, let me know.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题