2011-10-06 73 views
2

我試圖通過jquery請求更新cakePHP模型的字段。通過jquery更新cakephp模型字段

即使ajax調用成功,模型也不會在數據庫中更新。

<?php $Url = Router::url(array('controller'=>'coupons','action'=>'update_statut'),true); ?> 

    $.post('<?php echo $Url ?>', { id: id},function(data) 
     { alert("sucess"); 
    }).error(function() { alert("error"); }) 
     .complete(function() { alert("complete"); }); 

在控制器的一側,這裏的行動:

function update_statut(){ 
     Configure::write('debug', 0); 
     $this->autoRender = false; 
     if($this->RequestHandler->isAjax()) { 
      $this->Coupon->id= $this->params['form']['id']; 
      $this->Coupon->saveField('statut','terminé'); 
      } 

我甚至硬編碼中的id行動,以確保它與我的錶行配合。

任何建議

回答

1

你可能會想,如果檢查出你的U」已經做

var $components = array('RequestHandler'); 

可能是條件

+0

yess apearently處理程序的條件被搞砸了......我完全忘記了導入組件,謝謝你..傻我 – Genjuro

0

我不CakePHP中的親,但你可以嘗試

$this->Coupon->id = $this->params['form']['id']; 

通過

$this->Coupon->id = $this->params['data']['id']; 

更換?

你可以找到更多信息here

+0

沒有工作,我不認爲這部分有什麼問題,因爲試圖硬編碼$ this-> Coupon-> id = 1; (至少對應於我的表中的行ID),仍然沒有工作 – Genjuro

0

firstble嘗試使用$。獲得這樣的工作:

<?php $Url = Router::url(array('controller'=>'coupons','action'=>'update_statut'),true); ?> 

    $.get('<?php echo $Url ?>'+'/'+id, {},function(data) { 
      alert("sucess"); 
     }); 

和功能update_statut添加參數ID像這樣的:

function update_statut($id=null){ 
     Configure::write('debug', 0); 
     $this->autoRender = false; 
     if($this->RequestHandler->isAjax()) { 
      $this->Coupon->id= $id; 
      $this->Coupon->saveField('statut','terminé'); 
}