Which Submit Button was Clicked in CakePHP?

冷暖自知 提交于 2019-12-24 09:46:24

问题


Which Submit Button was Clicked in CakePHP?

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


       ?> 

回答1:


It's because you have created one form and you are submitting 2 different values on the "USER" form.

so it will redirected because action of the form is common.

To avoid it.. Using of 2 different forms are the best way.

Another way is to use javascript but I suggest to use 2 different forms.




回答2:


Typical solution to two submit buttons situation is to create a submit button (default action) and a regular button to handle another action. Than you can use the JavaScript to implement the behaviour of the second button, e.g. to set a value of a hidden field which contains the actual 'action' string:

echo $this->Form->create('User',array('action'=>'orders','type' =>'post'));
echo $this->Form->hidden('submit_action', array(id='submit_action')); ?>
echo $this->Form->submit('Ship',array('value'=>'Ship','id'=>'ship_button'));
echo $this->Form->button('Print',array('value'=>'Print','id'=>'print_button'));
echo $this->Form->end();

And the js:

<script>
var form = <get your form here>
var print_button = document.getElementById('print_button');
print_button.onclick = function() {
    // Set value of print button to the #submit_action hidden field
     document.getElementById('submit_action').value = print_button.value; 
    // Submit the form
    form.submit();
}
</script>



回答3:


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.

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         
    }


来源:https://stackoverflow.com/questions/9206910/which-submit-button-was-clicked-in-cakephp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!