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

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

问题描述

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

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

Form->create('Message',array('类型' =>'邮政','动作' =>'添加','onSubmit' =>'返回假;'));echo $this->Form->input('name', array('label' => 'From:'));echo $this->Form->input('text', array('label' => 'Message:'));echo $this->Js->submit('发布你的消息', array('动作' =>'添加','更新' =>'#留言板'));echo $this->Form->end();?><div id="message_board">...

这是控制器操作:

function add() {$this->autoRender = false;if($this->RequestHandler->isAjax()) {$this->layout = 'ajax';//此行新添加if(!empty($this->data)) {if($this->Message->save($this->data)){$this->Session->setFlash('您的消息已发布');}}}}

奇怪的是,当我提交表单时会发生什么是表单的精确副本,并且它包含的 div 被复制到 message_board div 的内部.奇怪的.

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

谢谢.

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

$(document).ready(function () {$("#submit-707957402").bind("click", function (event) {$.ajax({动作:添加",数据:$("#submit-707957402").closest("form").serialize(),数据类型:html",成功:功能(数据,文本状态){$("#message_board").html(data);},类型:帖子",url:"/messages"});返回假;});});

解决方案

它会加载default 布局.您必须使用以下行将布局更改为 ajax:

$this->layout = 'ajax';

您在 isAjax() 条件中插入该行.

你的 options 数组也有错误的格式.action 键应该在 url 键内.

$this->Js->submit('Post Your Message', array('网址' =>大批('动作' =>'添加'),'更新' =>'#留言板'));

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

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

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