1

I'm trying to create a form in CakePHP, in which there are 2 buttons: 'Accept' with value = 1, and 'Reject' 0. One button is generated with $this->Form->end() and another with $this->Form->submit(). On submission the value of field is_accept in the database should be updated, with 0 or 1 depending on which button the user choose to click. But I'm not sure how to set the values for the buttons and also how to save the value to it.

The form:

echo $this->Form->create('Order');
$options = array(
    'value' => '0',
    'class' => 'btn btn-primary btn-lg pull-right'
);
echo $this->Form->submit('Reject', $options);   

$options = array(
    'label' => __('Accept'),
    'class' => 'btn btn-primary btn-lg', 
    'value' => '1'
);
echo $this->Form->end($options); 
Alvin Mok
  • 323
  • 1
  • 14

1 Answers1

1

You can handle names of the buttons and then use a simple if statement to recognize which button was pressed.

<?php echo $this->Form->create('form_name'); ?>
<?php echo $this->Form->submit('btn_1', array('name' => 'btn')); ?>
<?php echo $this->Form->submit('btn_2', array('name' => 'btn')); ?>

/* Please don't add any js related with submit buttons else both will simply submit without any difference as $this->request->data['btn'] will not be present in the post data. */ Form->end(); ?>

if($this->request->data['btn'] == 'btn_1') {
 // is btn1 pressed
} else {
 // btn2 pressed
}
Navnish Bhardwaj
  • 1,687
  • 25
  • 39
  • Thank you! I wanted to generate both buttons the same way too. – Alvin Mok Sep 03 '15 at 13:50
  • The code you wrote is not valid. Form elements must not have the same name http://stackoverflow.com/questions/11111670/is-it-ok-to-have-multiple-html-forms-with-the-same-name – gmponos Sep 03 '15 at 15:16
  • @gmponos I'm trying to name two buttons differently and just check which value is set in php to determine which button is clicked.. i.e. `if(isset($this->request->data['btw'])) { ... }` There is probably a much better solution.. Could you please advise? Thanks. – Alvin Mok Sep 05 '15 at 11:54