红酒泡绿茶 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条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化