2014-06-09 36 views
0

好吧,所以我必须错过某些关键或基本的东西。我正在尝试创建一个使用JQuery/AJAX登录的登录元素。CakePHP 2.5 JQuery Ajax登录元素

在我的布局,我有:

//... 
    echo $this->Html->script('jquery'); // Include jQuery library 
    //... 
    if (AuthComponent::user('id')){ 
     echo $this->element('userElement',array('username'=>AuthComponent::user('username'))); 
    } else { 
     echo $this->element('loginElement'); 
    } 
    echo $this->Js->writeBuffer(); 

UserElement只是说“你好$用户名”,当我转到/用户/登录手工登录工作正常/

这里是loginElement.ctp,我认为问题是:

<?php 
    $data = $this->Js->get('#UserForm')->serializeForm(array('isForm' => true, 'inline' => true)); 
    $this->Js->get('#UserForm')->event(
     'submit', 
     $this->Js->request(
      array('action' => 'save'), 
       array(
        'update' => '#loadingdiv',//what to update while ajaxing 
        'before' => "$('#loading').fadeIn();",//before request 
        'complete' => "$('#loading').fadeOut();",//when request complete 
        'data' => $data, 
        'async' => true,  
        'dataExpression'=>true, 
        'method' => 'POST', 
       ) 
      ) 
     ); 
?> 
<div class="loadingdiv" id="loadingdiv"> </div> 
<div class="users form"> 
    <?php 
     echo $this->Session->flash('auth'); 
     echo $this->Form->create('User',array('action' => 'save', 'default' =>  false)); 
    ?> 
    <fieldset> 
    <?php 
     echo $this->Form->input('username'); 
     echo $this->Form->input('password'); 
    ?> 
    </fieldset> 
    <?php 
     echo $this->Form->end(__('Login')); 
     echo $this->Js->writeBuffer(); 
    ?> 
</div> 

表单在浏览器控制台中没有做任何事情,没有登录,没有失败的javascript调用....没有。我错过了什么?

回答

0

“米回答我的问题,这样,如果有人在这个问题别人无意中发现,他们可以看到什么是错的,如何解决它:

两个关键位置作品是:

//create the form 
echo $this->Form->create('User',array('action' => 'save', 'default' =>  false)); 

//bind the event 
$this->Js->get('#UserForm')->event(
//... 

我结合该事件的形式是“用户窗体”,但是当我创建的形式,我给它的“拯救”行动..所以形式名称蛋糕自动构建是“UserSaveForm “

当我更改绑定到“UserSaveForm”它的工作。

[修复这个“保存”看了之后注意奇怪,所以我改变了表单操作,绑定和控制功能,“登录”,所以我结束了与形式“UserLoginForm”曰好得多]