douchao0358 2013-09-23 18:27
浏览 40
已采纳

cakephp和ajax提交

I am not so good at ajax and jQuery stuff. In this I want to update my database table with checkbox value. Here code from ctp file

$(".chk").click(function(e){
           e.preventDefault();
           var id = $(this).parent().attr('id');
           $.ajax({
              type: 'post',
               url:'/Home/chktodo',
               data: 'id='+id,
               success : function(data) {

               }
           })
       });

foreach($View as $view):
    echo $view['Todolist']['details'];?> :To Do List
<div id = "<?= $view['Todolist']['id']?>">
<a id="todo" href="#">add to do</a>
<div id="clearNiPela-<?= $view['Todolist']['id'] ?>"></div>
</div>
<?php $todo = $view['Todo']; foreach($todo as $do): ?>
<div id="hours"> Hours :<?= $do['hours'];?></div>
<div id="description"> Description :<?= $do['description'];?></div>

<?= 
$this->Form->create('Todo',array('url' => '/Home/chktodo'));?>
<div id="<?='desc'.$do['id']?>">
<?=$this->Form->input('active',array('class' => 'chk','type' => 'checkbox','div'=>false))
.'<br />'.
$this->Form->end(); ?>
    </div>
<?php endforeach; endforeach; ?>

And here its code from My controler

public function chktodo() {
            if($this->request->is('ajax')) {
            $this->loadModel('Todo');
            $this->request->data['Todo']['id'] = $this->request->query('id');
                if($this->Todo->save($this->request->data)) {
                $this->Session->setFlash('Done','flash',array('alert' => 'info'));
                $this->redirect('/');
            }
          }
        }

Right now I have 3 Checkboxes as database values .

Issue 1 I can not change data in db. I mean it is not updating data base with value of checkbox

  • 写回答

1条回答 默认 最新

  • douleng0794 2013-10-26 10:39
    关注

    Use [.chnge][1] function for checkbox

    $('.chk').change(function(event) { //Todo active inactive
               event.preventDefault();
               var id = $(this).parent().attr('id');
               if($(".chk").is(':checked'))
                    var active = 1;  // checked
               else
                    var active = 0;  // unchecked
                $.ajax({
                  type: 'post',
                  $.ajax({
                  type: 'post',
                   url:'/Home/chktodo',
                   data: 'id='+id,
                   success : function(data) {
                        //some action
                        }
               });
           });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 webSocket可以接TCP socket接口吗
  • ¥60 mpi并行出错,CFD++计算
  • ¥15 c#:vsto,powerpoint的外接程序中换主题颜色
  • ¥15 状态机/汽车转向灯/Sateflow
  • ¥15 这个有点复杂 有没有人看看
  • ¥15 用python如何确定子孙元素在父元素中的位置
  • ¥15 obj文件滤除异常高程
  • ¥15 用mathematicas或者matlab计算三重积分
  • ¥15 Loop unrolling的runtime计算
  • ¥100 NVMe-oF的Target端,开启attr_offload后,测试失败。