单击jquery按钮+发送没有表单的数据 - 书签

cho*_*wwy 9 ajax jquery codeigniter codeigniter-2

我正在开发书签功能,用户点击jQueryui按钮,然后将某些信息发送到数据库.但我没有使用表单,因为没有用户输入的信息.

我从会话数据中提取用户的ID,我正在发送一个URI段(URL的一部分)

使用codeigniter/php.

我正在试图弄清楚要放在ajax/post函数的数据部分中的内容,因为没有输入表格/没有数据,以及如何处理控制器的"提交"部分.

调节器

function addBookmark(){

        if ($this->input->post('submit')) {

            $id = $this->session->userdata('id');               
            $bookmark = $this->uri->segment(3, 0);

            $this->bookmarks_model->postBookmark($id, $bookmark);
        }

    }
Run Code Online (Sandbox Code Playgroud)

模型

function postBookmark() {

     $data = array(
            'user_id' => $user_id,
            'bookmark_id' => $bookmark,
    );

    $this->db->insert('bookmarks', $data);

    }
Run Code Online (Sandbox Code Playgroud)

HTML

<button class="somebutton">Add bookmark</button>
Run Code Online (Sandbox Code Playgroud)

jQuery的

$('.somebutton').click(function() { 

            $.ajax({
                url: 'controller/addBookmark',
                type: 'POST',
                data: ???,
                success: function (result) {
                  alert("Your bookmark has been saved");
                }
            });  

    });
Run Code Online (Sandbox Code Playgroud)

Bro*_*omb 8

你的问题是你正在检查args中的submit密钥POST.您可以通过发送data: {submit:true}或删除if语句并仅处理POST请求来伪造它

$('.somebutton').click(function() { 

        $.ajax({
            url: 'controller/addBookmark',
            type: 'POST',
            data: {'submit':true}, // An object with the key 'submit' and value 'true;
            success: function (result) {
              alert("Your bookmark has been saved");
            }
        });  

});
Run Code Online (Sandbox Code Playgroud)