如何在CakePHP中使用Js->submit()?

发布于 2024-09-26 17:22:59 字数 1764 浏览 5 评论 0原文

我试图在 CakePHP 应用程序中为留言板创建一个简单的 Ajax 表单,但我无法弄清楚如何正确使用 Js->submit() 函数通过 Ajax 提交表单。

这是我视图中的表单代码:

<?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>

这是控制器操作:

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');
         }
     }
 }
}

奇怪的是,当我提交表单时发生的情况是表单的精确副本,并且其包含的 div 在 message_board div 内重复。诡异的。

显然我错过了一些东西(或几件事)。如果有人有任何想法,或者知道如何使用它的良好信息来源,我们将不胜感激。

谢谢。

更新:我尝试将新行 $this->layout = 'ajax'; 添加到控制器(见上文),但没有效果。这是 CakePHP 的 jquery 输出,以防有人知道发生了什么。

$(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;
    });
});

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;
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-10-03 17:22:59

发生的情况是它加载了默认布局。您必须使用以下行将布局更改为 ajax

$this->layout = 'ajax';

将该行插入到 isAjax() 条件中。

此外,您的 options 数组的格式也不正确。 action 键应位于 url 键内。

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

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'
    )
);
我乃一代侩神 2024-10-03 17:22:59

我参加聚会已经迟到了!但不久前,大约三个月前,我遇到了这个问题。结果是我没有授予用户访问该操作的权限。解决方案是在 AppController beforeFilter() 中添加一些代码,例如:

function beforeFilter(){
    $this->Auth->allow('my_actions_name');
}

但是我不太确定为什么它会重新渲染父 div,有时甚至整个页面,但我的猜测是,蛋糕被设置为每当该操作不可访问/允许,发生一些路由,并且可能执行索引或其他默认操作,这会导致该操作在您的 ajax 视图中呈现。

如果您检查浏览器网络选项卡(在 Chrome 中)内的请求响应,很明显返回的响应与正在呈现的内容相同。然而,我是一个完全的新手,答案是基于猜测。

如果有人能给出更好的解释(请教我们),我将是第一个投票的人:)

到那时,快乐编码

I am very late to the party! But a while back, about 3months ago I faced this problem. And what it turned out to be was that I was not giving permission to the user to access that action. The solution was to add some code in the AppController beforeFilter(), something like :

function beforeFilter(){
    $this->Auth->allow('my_actions_name');
}

However I am not really sure why it re-renders the parent div, or sometimes the whole page, but my guess is, that cake is set so that whenever the action is not accessible/allowed, some routing takes place and perhaps index or other default actions, are executed, which causes that to be rendered within your ajax view.

If you inspect the request response inside the browser's network tab( in chrome), it becomes very clear that the response returned is the same content that is being rendered. However, I am a complete newbie and the is answer is based on speculation.

If anyone can give a better explanation(please teach us), I will be the first to upvote :)

Till then, Happy coding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文