0

我只是想从表单提交中调用一个函数(这也是从 ajax 调用的)。

我想调用 add 方法,但它是总是调用的索引。

我使用 ajax 提交表单的 ajax 视图:

<?php $count = count($files); ?>
<div id="message"></div>
<i><?= __('Nombre de fichiers liés : ') . $count ?></i>
<?= $this->Form->create('FilesManager', array('enctype' => 'multipart/form-data', 'url' => array('action' => 'add'))); ?>
<?= $this->Form->input('file', array('type' => 'file', 'label' => false, 'class' => 'form-control')); ?>
<?= $this->Js->submit('Envoyer', array('update' => '#message', 'div' => false, 'type' => 'json', 'async' => false)); ?>
<?= $this->Js->writeBuffer(); ?>

我也试过:

<?= $this->Form->create(null, array('enctype' => 'multipart/form-data', 'url' => array('controller' => 'FilesManagers', 'action' => 'add'))); ?>

<?= $this->Form->create('FilesManager/add', array('enctype' => 'multipart/form-data')); ?>

编辑

$this->Form->create('FilesManager', array('action' => 'add'))

不要为我工作,但生成的表格如下所示:

<form action="/agralis/files_managers/add" enctype="multipart/form-data" id="FilesManagerIndexForm" method="post" accept-charset="utf-8">...<input id="submit-838644811" type="submit" value="Envoyer"><script type="text/javascript">
//<![CDATA[
$("#submit-838644811").bind("click", function (event) {$.ajax({async:false, data:$("#submit-838644811").closest("form").serialize(), dataType:"html", success:function (data, textStatus) {$("#message").html(data);}, type:"post", url:"\/agralis\/FilesManagers"});
return false;});
//]]>
</script></form>

我可以看到表单动作看起来不错(当我在浏览器 url 中复制/粘贴动作时,他找到了方法)但是从 ajax 提交按钮调用的 url 是错误的!我该如何改变呢?

4

1 回答 1

1

助手们独立工作

Js助手不知道Form助手,它们不相互交互,所以表单 URL 对助手来说是未知的Js

问题的解决方案相当简单,只需查看文档:

http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::submit

submit()方法接受一个url选项,您可以在其中指定一个 URL,以防基于当前请求的默认 URL 不适合您。

$this->Js->submit('Envoyer', array(
    'url' => array('action' => 'add')
    // ...
));

不支持文件上传

您将遇到的下一个问题是文件上传不起作用。这不是问题的一部分,但我会提出这个主题。

帮助程序不支持生成处理文件上传所需的JsJavaScript,它需要利用旧版浏览器所缺乏XMLHttpRequest的Level 2 ( xhr2) 功能。

您必须编写自定义 JavaScript 来处理它。可以在整个网络上找到有关此的说明和教程。

于 2014-07-07T16:18:39.260 回答