2015-09-03 28 views
1

我試圖在CakePHP中創建一個表單,其中有兩個按鈕:'接受'值= 1,'拒絕'0.一個按鈕與$this->Form->end()和另一個與$this->Form->submit()。提交時,數據庫中字段is_accept的值應更新,0或1取決於用戶選擇單擊哪個按鈕。但我不知道如何設置按鈕的值以及如何將值保存到它。兩個在CakePHP中提交不同值的按鈕

形式:

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); 

回答

1

您可以處理按鈕的名稱,然後用一個簡單的if語句來識別哪個按鈕被按下。

<?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')); ?> 

/*請不要加些別的都提交按鈕相關只會提交任何JS不通過$ this->請求 - >數據[「BTN」]任何差異不會出現在後數據。 */ Form-> end(); ?>

if($this->request->data['btn'] == 'btn_1') { 
// is btn1 pressed 
} else { 
// btn2 pressed 
} 
+0

謝謝!我想以同樣的方式生成兩個按鈕。 –

+0

您所寫的代碼無效。表單元素不能有相同的名稱http://stackoverflow.com/questions/11111670/is-it-ok-to-have-multiple-html-forms-with-the-same-name – gmponos

+0

@gmponos我試圖以不同的方式命名兩個按鈕,只需檢查在php中設置了哪個值,以確定哪個按鈕被點擊。即ie [isset($ this-> request-> data ['btw'])){...}'There可能是一個更好的解決方案..你能提供建議嗎?謝謝。 –