cakephp 2 ajax form

前端 未结 2 1728
生来不讨喜
生来不讨喜 2020-12-19 14:33

I\'m having trouble building an ajax form in cakephp 2 which obviously has changed a lot since 1.3.

I\'m using the following code:

相关标签:
2条回答
  • 2020-12-19 15:17

    Try this in your view file:

    <?php
    
        $data = $this->Js->get('#CommentSaveForm')->serializeForm(array('isForm' => true, 'inline' => true));
        $this->Js->get('#CommentSaveForm')->event(
              'submit',
              $this->Js->request(
                array('action' => 'save'),
                array(
                        'update' => '#commentStatus',
                        'data' => $data,
                        'async' => true,    
                        'dataExpression'=>true,
                        'method' => 'POST'
                    )
                )
            );
        echo $this->Form->create('Comment', array('action' => 'save', 'default' => false));
        echo $this->Form->input('Comment.comments_name');
        echo $this->Form->input('Comment.comments_email');
        echo $this->Form->input('Comment.comments_text');
        echo $this->Form->end(__('Submit'));
        echo $this->Js->writeBuffer();
    
    ?>
    

    NOTE: #CommentSaveForm is ID generated by CakePHP, If you have your own then use that

    0 讨论(0)
  • 2020-12-19 15:26

    You want to show the loading image, use 'before' and 'complete' in $this->Js->request():

    <?php
        $this->Js->request(array('action' => 'save'), array(
           'update' => '#commentStatus',
           'data' => $data,
           'async' => true,    
           'dataExpression' => true,
           'method' => 'POST',
           'before' => "$('#loading').fadeIn();",
           'complete' => "$('#loading').fadeOut();",
       ));
    ?>
    
    0 讨论(0)
提交回复
热议问题