cakephp两个提交按钮和重定向

时间:2012-07-31 07:07:48

标签: forms cakephp submit

尝试创建一个在cakephp中有两个提交按钮的表单。

点击后,

task_1提交按钮会在地址栏'/ fields / add'中创建,并发送添加到数据库的信息。

单击

task_2提交按钮会在地址栏'/ fields / add / add'中创建,并将输入的信息发送到数据库。

task_1旨在允许用户继续创建新字段,其中任务2旨在将它们带到不同的页面,此时我们只是随机选择了一个页面。

这是控制器中添加功能的代码

 function add(){

    $this->set('title_for_layout', 'Please Enter Your Invoice Headings');
    $this->set('stylesheet_used', 'style');
    $this->set('image_used', 'eBOXLogo.jpg');   

    $this->Session->setFlash("Please create your required fields.");

    if($this->request->is('post')){

    $this->Field->create(); 

    if ($this->Field->save($this->request->data)) 
    { 
        if($this->params['form']['task_1'] == "task_1") 
            { 
                $this->Session->setFlash('The field has been saved');  
                $this->redirect( array('controller' => 'Fields','action' => 'add'));
            } 
            if($this->params['form']['task_2'] == "task_2") 
            { 
                $this->Session->setFlash('The template has been saved'); 
                $this->redirect( array('controller' => 'Invoices','action' => 'add'));
            } 


    }
    else
    {
        $this->Session->setFlash('The field could not be saved. Please, try again.'); 
    } 
    } 

  }

这是添加视图的代码

<form enctype="multipart/form-data" method="post" action="add/"> 
        Name: <input type ="text" name="name" /> <br />
        Description: <input type ="text" name="description" /> <br />
        Account ID: <input type ="number" name="accounts_id" /> <br />
        <br />

        <input type="submit" name="task_1" value="Continue adding fields" /> 
        <input type="submit" name="task_2" value="Finish adding fields" /> 
</form> 

1 个答案:

答案 0 :(得分:2)

    echo $this->Form->create('Field');
    echo $this->Form->input('name');
    echo $this->Form->input('description');
    echo $this->Form->input('account_id');//this would be the conventional fk fieldname
    echo $this->Form->button('Continue adding fields', array('name' => 'type_1'));
    echo $this->Form->button('Finish adding fields', array('name' => 'type_2'));
    echo $this->Form->end();