2

我是 Zend 的新手,我想用一个按钮做一个简单的页面。我没有得到的东西(尽管阅读了很多在线资料)是如何在控制器中创建相应的动作。

我有以下视图脚本 index.phtml:

<?php

echo $this->button1

另一个名为 add.phtml 的脚本:

<?php echo $this->json($this->data);

和一个控制器:

class blabla extends Zend_Controller_Action {
    public function indexAction() {

        $addButton = new Zend_Form_Element_Button("button",array('class' => 'btn_op', 'action'=>'add' , 'id' => 'addbtn')); //button
        $addButton->setLabel("Add >>");
        //$addButton->setAttrib('onclick', 'alert("Hello!")'); //not what I want
        $this->view->button1 = $addButton->render(null);


    }

    public function addAction() {

        echo 'I want my code to go here on click';

    }

}

谢谢 :)

4

3 回答 3

2

你能试试这个吗

class blabla extends Zend_Controller_Action {
    public function indexAction() {

        $addButton = new Zend_Form_Element_Button("button",array('class' => 'btn_op', 'action'=>'add' , 'id' => 'addbtn')); //button
        $addButton->setLabel("Add >>");

        $url = $this->view->url( array('controller' => 'blabla', 'action' => 'add'), 'default', false);
        $addButton->setAttrib('onclick', 'window.location.replace("'.$url.'");');

        $this->view->button1 = $addButton->render(null);
    }
}
于 2013-08-21T04:40:51.460 回答
0

if you want your button from one action to go to the another action , You Can always use redirect helpers..

i assumed you are using Zf 1 and not zf 2 from your code..

you can directly redirect with following code,

 $this->_helper->redirector('your_action', 'Your_controller');

in your case

class blabla extends Zend_Controller_Action {
public function indexAction() {

    $addButton = new Zend_Form_Element_Button("submit",array('class' => 'btn_op', 'action'=>'add' , 'id' => 'addbtn')); //button
    $addButton->setLabel("Add >>");
    //$addButton->setAttrib('onclick', 'alert("Hello!")'); //not what I want
    $this->view->button1 = $addButton->render(null);
    $this->_helper->redirector('add', 'Your_controller');


}

public function addAction() {

    echo 'The Button was Pressed';

}

}

I am sure that will work , you can also check if the button is pressed or not, if you want , but this will basically submit the button and then redirect to your add action,..

于 2013-08-20T17:05:59.123 回答
0

您实际上只是想重定向还是打算让表单提交数据?鉴于您的问题的上下文并且该按钮标记为“添加>>”,我希望您要提交。

在这种情况下,您应该使“添加”按钮成为 Zend_Form_Element_Submit 的实例,并将其包装在具有指向 addAction() 方法的表单的“动作”属性的表单中。例如:

class blabla extends Zend_Controller_Action {

    public function indexAction() {

        $addButton = new Zend_Form_Element_Submit('add');
        $addButton->setLabel('Add >>');
        $addButton->setAttrib('id', 'addbtn')

        $this->view->button1 = $addButton->render();

    }

    public function addAction() {

        echo 'I want my code to go here on click';

    }

}

对应的视图脚本:

<form action="<?php echo $this->url( array('controller' => 'blabla', 'action' => 'add'), 'default', false) ?>" method="post">
  <?php echo $this->button1 ?>
</form>
于 2013-08-21T07:40:50.337 回答