如何使用Js->提交()在CakePHP中? [英] How to use Js->submit() in CakePHP?

查看:165
本文介绍了如何使用Js->提交()在CakePHP中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去创建一个简单的Ajax形式在CakePHP的应用程序中的留言板,但我不能为我的生活弄清楚如何正确使用Js->提交()函数通过Ajax提交表单

继承人在我看来,形式为code:

 < PHP

 回声$这个 - >形式 - >创建(消息,阵列(
  '类型'=> 邮报,
  '行动'=> '加',
  的onsubmit => '返回false;'
 ));

 回声$这个 - >形式 - >输入(名称,阵列('标签'=>发件人:));
 回声$这个 - >形式 - >输入('文字',阵列('标签'=>'消息:'));

 回声$这个 - > Js->提交(发布消息,阵列(
  '行动'=> '加',
  更新=> '#留言板'
 ));

 回声$这个 - >形式 - >结束();

?>

< D​​IV ID =message_board>
    ...
< / DIV>
 

这里是控制器动作:

 函数add(){
 $这个 - > autoRender = FALSE;
 如果($这 - > RequestHandler-> isAjax()){
     $这个 - >布局='阿贾克斯'; //这个线新增
     如果(空($这个 - >!数据)){
         如果($这个 - > MESSAGE->节省($这个 - >数据)){
             $这个 - >会话级> setFlash(您的留言已经发布');
         }
     }
 }
}
 

奇怪的是,当我提交表单的格式的精确拷贝和它的容器div被复制里面的message_board格会发生什么。怪异。

很显然,我失去了一些东西(或几件事情)。如果任何人有任何想法,或其他人知道的关于如何使用它的信息的良好来源,这将是更AP preciated。

感谢。

更新:我尝试添加新行 $这个 - >布局='阿贾克斯'; 到控制器(见上文),但没有任何效果。下面是CakePHP的jQuery的输出,以防可能告诉别人怎么回事。

  $(文件)。就绪(函数(){
    $(#提交-707957402)。绑定(点击,函数(事件){
        $阿贾克斯({
            动作:增加,
            数据:$(#提交-707957402)最近(形式)序列化()。
            数据类型:HTML,
            成功:功能(数据,textStatus){
                $(#message_board)的HTML(数据);
            },
            类型:后,
            网址:\ /邮件
        });
        返回false;
    });
});
 

解决方案

什么情况是,它加载了默认的布局。你必须改变布局,以 AJAX 用下面的一行:

  $这个 - >布局='阿贾克斯';
 

您插入 isAjax()的条件内该行。

此外,您的选项阵列格式错误。该动作键应该是 URL里面键。

  $这个 - > Js->提交(发布消息,阵列(
        'URL'=>阵列(
            '行动'=> '加'
        ),
        更新=> '#留言板'
    )
);
 

Im trying to create a simple Ajax form for a message board in a CakePHP application, but I can't for the life of me figure out how to properly use the Js->submit() function to submit a form via Ajax.

Heres the form code within my view:

<?php

 echo $this->Form->create('Message',array(
  'type' => 'post', 
  'action' => 'add',
  'onSubmit' => 'return false;'
 ));

 echo $this->Form->input('name', array('label' => 'From:'));
 echo $this->Form->input('text', array('label' => 'Message:'));

 echo $this->Js->submit('Post Your Message', array(
  'action' => 'add',
  'update' => '#message_board'
 ));

 echo $this->Form->end();

?>

<div id="message_board">
    ...
</div>

And here is the controller action:

function add() {
 $this->autoRender = false; 
 if($this->RequestHandler->isAjax()) {
     $this->layout = 'ajax'; //THIS LINE NEWLY ADDED
     if(!empty($this->data)) {
         if($this->Message->save($this->data)) {
             $this->Session->setFlash('Your Message has been posted');
         }
     }
 }
}

Oddly, what happens when I submit the form is an exact copy of the form and its containing div is duplicated INSIDE the message_board div. Weird.

Obviously I'm missing something (or several things). If anyone has any idea, or else knows a good source of information on how to use it, it would be much appreciated.

Thanks.

UPDATE: I tried adding the new line $this->layout = 'ajax'; to the controller (see above), but it had no effect. Here is the jquery output by CakePHP, incase that might tell somebody whats going on.

$(document).ready(function () {
    $("#submit-707957402").bind("click", function (event) {
        $.ajax({
            action:"add", 
            data:$("#submit-707957402").closest("form").serialize(), 
            dataType:"html", 
            success:function (data, textStatus) {
                $("#message_board").html(data);
            }, 
            type:"post", 
            url:"\/messages"
        });
        return false;
    });
});

解决方案

What happens is that it loads the default layout. You'll have to change the layout to ajax with the following line:

$this->layout = 'ajax';

You insert that line inside your isAjax() condition.

Also your options array has the wrong format. The action key should be inside the url key.

$this->Js->submit('Post Your Message', array(
        'url' => array(
            'action' => 'add'
        ),
        'update' => '#message_board'
    )
);

这篇关于如何使用Js-&GT;提交()在CakePHP中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆