开发者

Zend passing variables between actions?

I'm not sure how to do this: Once a row is added with muy 'add' action, i want to send the user to my 'view' action so they can see the row that was submitted. But seeing as it was just submitted, I want to show the user a succes message that their data was submitted.

I show my rows this way: url.com/view/32

I only want to开发者_JAVA百科 show the succes message if they just submitted something, not when they're only reading data.

So I want to pass something like $submitted = true to my view Action, but I have no idea how to do this. Any suggestions?

Also, another way I thought of, was checking if the visitor is coming from my add action, and if so, show the success message. Again, not sure how to do this though...


The flashMessenger action helper is designed for displaying messages in the next request.

It uses session to store the information.


after researching Zend several issues here and elsewhere ive come to the conclusion that answering zend questions without a step by step list really sucks so here it goes:

in your form.html:

<input type="text" name="field[input1]" id="input1" />
<input type="text" name="field[input2]" id="input2" />

in your controller.php:

public function addAction(){
        if ($this->getRequest()->isPost()){         
            $fields= $this->_getParam("field");         
            $this->view->msg = ($this->myDB->add($fields) ? "Add succeeded" : "Add failed");
            header("location: http://mysite.com/thepageiwantutogoto");
        }
    }

in your view.phtml:

<?php echo $this->msg ?>

btw $this->myDB->add($fields) is where you have your insert statement to the db


You could simply render the view when the addAction() succeeds:

return $this->render('view');

So in persudeo:

addAction():

  • If the request is not a POST show a blank add form.
  • If the page is a POST, but the form isn't valid redisplay the form.
  • If the page is a POST, the form was valid, render the view.

You would need to pass everything to the view script you do for your viewAction(), but you could also pass a message, which will now get to the view script.

In your view.phtml simply use a php snipet to test if (isset($this->message))echo "Data Added...."; This way when you use the normal viewAction() it won't show a message. This will leave the user at the addAction() url though.

If you wanted to move the user over to a different URL see the answeer here


You could send the data through a session variable (you can use Zend_Session to handle that). When you reach your view, you check if the session variable is set, if it is, you display the message and void the variable. This way if the user refresh the page, the message isn't displayed again (which is not the case of url params, the message will be displayed again). If it is not set, you simply dont display the message.

I used this system to display a message on the login screen if a user who is not logged try to access a secured section and it work very well


try something like this::

    if ($this->getRequest()->isPost()) {
        if ($form->isValid($_POST)) {
            $data = $form->getValues();
            $M = new Model_Xyz();
            $M->insert($data);
            //now the magic, pass success param to to the viewAction()
            return $this->_forward('view', null, null, array ('success' => 1));
        }
    }

now the success param is passed to the viewAction() just as it was in the URL, so you can access it in the controller and the view. Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜