CakePHP 表单选择作为参数

发布于 2024-10-21 12:03:39 字数 988 浏览 1 评论 0原文

我的 index.ctp 视图中有以下代码来创建表单:

<?php 
    echo $this->Form->create(false,array('url' => array('controller' => 'admins', 'action' => 'edit_gallery')));

    echo $this->Form->input('name', array('options' => $array,'empty' => 'Select a gallery'));

    echo $this->Form->end(__('Submit', true));
?>

Thi 代码创建一个项目下拉列表,每个项目都有一个关联的数字作为值。 在我的 admins_controller 中,我执行了 edit_gallery 操作,这与您烘焙项目时的情况完全一样,只是我将典型的 edit 更改为 >edit_gallery

我想要的是:用户从列表中选择一项,然后单击“提交”,然后他将进入 edit_gallery.ctp 视图,其中包含一个用于编辑该项目信息的表单数据库并更新它。我的问题是,不是这样做,而是当用户单击提交时,会在数据库中创建一个新项目,并且它甚至不显示 ctp 视图。

一般来说,我的问题是:用户单击“提交”后,如何在登陆页面中获取表单的选定选项?

编辑 理想情况下,我想要的是,当用户单击“提交”时,它会发送类似 admins/edit_gallery/x 的请求,其中 x 是与用户所做的选择,而不向操作发送任何其他数据。我不知道这是否可能。

谢谢你!

I have the following code in my index.ctp view to create a form:

<?php 
    echo $this->Form->create(false,array('url' => array('controller' => 'admins', 'action' => 'edit_gallery')));

    echo $this->Form->input('name', array('options' => $array,'empty' => 'Select a gallery'));

    echo $this->Form->end(__('Submit', true));
?>

Thi codes creates a dropdown list of items, each one with an associated number as value.
In my admins_controller I have the edit_gallery action implemented exactly as it comes when you bake a project, only that I changed the typical edit to edit_gallery.

What I want is the following: the user selects one item from the list, then clicks 'Submit', and he's taken to the edit_gallery.ctp view, with a form to edit the information of that item in the database and update it. My problem is that, instead of doing this, what happens is that when the user clicks Submit, a new item is created in the database, and it doesn't even show the ctp view.

In general, my question would be: how can I get the selected option of the form in the landing page after the user clicks 'Submit'?

Edit
Ideally, what I would want is that, when the user clicks 'Submit', it would send a request like admins/edit_gallery/x where x would be the value associated to the selection made by the user, without sending any other data to the action. I don't know if that's possible.

Thank you!

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

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

发布评论

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

评论(1

枕梦 2024-10-28 12:03:40

CakePHP 烘焙的编辑方法默认检查 data 是否不为空,换句话说,是否已提交表单,然后更新该记录。

因此,当您从表单重定向到 *edit_gallery* 时,data 属性不为 null,这就是在数据库中创建新项目的原因。

有很多方法可以解决这个问题。其中之一是从 *edit_gallery* 方法中删除该检查,创建另一个方法,例如 *save_gallery*,然后从 *edit_gallery.ctp* 调用该方法。

所以 *edit_gallery.ctp* 表单看起来像这样:

<?php 
    echo $this->Form->create(false,array('url' => array('controller' => 'admins', 'action' => 'save_gallery')));

    (your form info)

    echo $this->Form->end(__('Submit', true));
?>

The edit method that CakePHP bakes check by default if data is not null, in other words, if a form has been submitted, and then updates that record.

So, when you redirect to *edit_gallery* from a form, the data property is not null, therefore the reason for the new item creation in the database.

There are many ways to solve this. One of them is to remove that check from the *edit_gallery* method, create another method like *save_gallery*, and call that method from *edit_gallery.ctp*.

So the *edit_gallery.ctp* form would look something like:

<?php 
    echo $this->Form->create(false,array('url' => array('controller' => 'admins', 'action' => 'save_gallery')));

    (your form info)

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