在CakePHP中单击了哪个提交按钮? [英] Which Submit Button was Clicked in CakePHP?

查看:86
本文介绍了在CakePHP中单击了哪个提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**在Cakephp中只有1个动作的相同表单上的2个按钮的解决方案是什么? **

** what is solution for 2 buttons on same form with only 1 action in cakephp? **


          i have following code,1 form contain 2 buttons print & ship,if i click 
   print  button,page is redirected on printorder page but problem is , if i click ship
   button it is not working & page is redirected on printorder.




      ========================================================================== 


     <?php // in index.ctp file

    echo $this->Form->create('User',array('action'=>'orders','type' =>'post'));
    echo $this->Form->submit('Ship',array('name'=>'user','value'=>'Ship','id'=>'1'));
    echo $this->Form->submit('Print',array('name'=>'user','value'=>'Print','id'=>'2'));
    echo $this->Form->end();
    ?>

      <?php // in UserController
          public function orders() 
              {
            if($this->params->data['form']['user'] = "Print" )
             {
          $this->redirect(array('action'=>'printorder'));
             }
            if($this->params->data['form']['user'] = "Ship")
            {
          $this->redirect(array('action'=>'shiporder')); 
             }
            }


       ?> 


推荐答案

我按照以下方式投票(未选中的解决方案) 两个提交按钮以一种形式,建议对每个提交按钮应用不同的值,然后检查提交的内容。

I followed the top voted (not selected solution) from Two submit buttons in one form, which suggested applying different values to each submit button, and checking those on submit.

但是,尽管设置了'value'键入 $ this-> Form-> submit $ options 数组中的code在生成的元素上设置。因此,我遵循了的建议,并使用名称 值键。

However, CakePHP didn't easily play with this technique, as despite setting 'value' key in the $options array of $this->Form->submit, no value was set on the generated element. So, I followed the suggestions from here, and used the 'name' value key.

view.ctp

    $this->Form->submit('Submit 1', array('name' => 'action1'));
    $this->Form->submit('Submit 2', array('name' => 'anotherAction'));

controller.php

    if (array_key_exists('action1', $this->request->data)) {
        /** setup any data you want to retain after redirect 
          *  in Session, Model, or add to redirect URL below
          */
        // redirect to another action
        $this->redirect(array('action' => 'myOtherAction'));
    } else if (array_key_exists('anotherAction', $this->request->data)) {
        // do something with anotherAction submit         
    }

这篇关于在CakePHP中单击了哪个提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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