如何使用ajax保存cakephp中的复选框值?

时间:2021-01-13 13:43:31

I want to save the values of the checkbox using ajax.

我想使用ajax保存复选框的值。

Here is my current code that I wish to update to use ajax.

这是我希望更新以使用ajax的当前代码。

here is the view

这是观点

    <?php $i = 0;
        foreach ($summary as $sum) {
            ?>
            <tr class="font-size12">
                <td><p><?php echo $sum['posts']['title']; ?></p></td>
                <td class="width450"><p><?php echo $sum['posts']['content']; ?></p></td>
                <td><p class="margin-left28"><?php echo $sum['members']['username']; ?></p></td>
                <td><p><?php echo $sum['posts']['deadline']; ?></p></td>
                    <?php echo $this->Form->create("Posts", array("action" => "update_checkbox")) ?>
                <td>
                    <?php
                    echo $this->Form->input('Post.' . $i . '.id', array("type" => "hidden", "label" => false, "value" =>
                        $sum['posts']['id']))
                    ?>
                    <?php
                    echo $this->Form->input('Post.' . $i . '.done', array("type" => "checkbox", "label" => false, "value" => "1", "id" => "idCheck[]", "onclick" => "getboxes()"))
                    ?>
                </td>
            </tr>

            <?php $i++;
        }
        ?>
    </table>
 <?php echo $this->Form->end() ?>

the javascript

function getboxes(){
    $("idCheck[]").click(function(){
        $.ajax({
            url: '../../../../Controller/PostsController',
            data: { action: 'checkingBox' },
            type: 'post'
//            ,
//            success: function(output) {
//                alert(output);
//            }
        });
    });
}

the controller

public function update_checkbox() {
        //    debug($this->data);
        $var = $this->Post->saveCheckBox($this->data);
        $this->set("result", $var);
    }

the model

 public function saveCheckBox($checkbox) {
        debug($checkbox);
        $this->saveAll($checkbox['Post']);
    }

edit: I added a listener in the checkbox

编辑:我在复选框中添加了一个监听器

1 个解决方案

#1


0  

According to your current code, there are two ways to store check-boxes values into the database. Either You will have to manipulate the posted data received into your ajax_method() and make it the array like below

根据您当前的代码,有两种方法可以将复选框值存储到数据库中。要么你必须操纵收到的ajax_method()中发布的数据,并将其作为下面的数组

[Post] => array(
                [0] => ....
                [1] => ....
                );

Then you c an use $this->Post->saveAll($this->request->data);

然后你用一个$ this-> Post-> saveAll($ this-> request-> data);

or you can use the following code to save checkbox values into the database.

或者您可以使用以下代码将复选框值保存到数据库中。

echo $this->Form->input('Case.CHECK_ID][', array('type' => 'checkbox'));

Then you can use saveAll() method.

然后你可以使用saveAll()方法。

#1


0  

According to your current code, there are two ways to store check-boxes values into the database. Either You will have to manipulate the posted data received into your ajax_method() and make it the array like below

根据您当前的代码,有两种方法可以将复选框值存储到数据库中。要么你必须操纵收到的ajax_method()中发布的数据,并将其作为下面的数组

[Post] => array(
                [0] => ....
                [1] => ....
                );

Then you c an use $this->Post->saveAll($this->request->data);

然后你用一个$ this-> Post-> saveAll($ this-> request-> data);

or you can use the following code to save checkbox values into the database.

或者您可以使用以下代码将复选框值保存到数据库中。

echo $this->Form->input('Case.CHECK_ID][', array('type' => 'checkbox'));

Then you can use saveAll() method.

然后你可以使用saveAll()方法。