views:

44

answers:

2

For some reason my zend application is redirecting when it's not supposed to.

The index view just shows the form. The next view shows some information from a database based on the information supplied by the form. The user then clicks on a confirm button which sends them to anotherAction which constructs and sends an email. I've got a try/catch block where it tries to send the email. If successful, set one variable, otherwise set another.

Psudo-code:

public function indexAction()
{
 $form = new Form();
 if(!empty($_POST))
 {
  if($form->isValid($_POST))
  {
   $this->_helper->FlashMessenger($_POST); //store $_POST in temporary session
   //show a different view
   //do something else
  }
 }
$this->view->form = $form;
}

So I'm instantialising a form, if the user posts the form then validate it and store the session variables in a flash session and show a differnet view. In all cases show the form (if it has been sent then it will be auto-populated).

public function anotherAction()
{
 $messages = $this->_helper->FlashMessenger->getMessages();
 if (!empty($messages[0]))
 {
  $post = $messages[0];
  $mail = new My_Mail();
  //set up email
  try
  {
   $mail->send();
   $success = true;
  }
  catch (Zend_Exception $e)
  {
   $success=false;
   //save $e to log
  }
  $this->view->success = $success;
 }
 else
 {
  //if not sent here by /page, then redirect to it.
  $this->_helper->redirector->goToRouteAndExit(array(), 'page', true);
 }
}

In this action, I'm checking for the session. If it's got the form data in it, then try to send an email and set variable; otherwise redirect back to the form. The view variable is used to show one message or the other (email sent / email failed to send).

Here's the problem: It redirects back to the form anyway. The form is submitted and confirmed, we go to the next page where it does something then it redirects back to the page with the form on it. Why is it doing this and how do I stop it?

[edit] The plot thickens: If I open the confirm link in a new tab it will run and stop correctly. If I do it in the same tab/window it runs twice and redirects.

[another edit] I have removed all the code in the second action. The code is now:

public function anotherAction()
{
 $messages = $this->_helper->FlashMessenger->getMessages();
 if (!empty($messages[0]))
 {
  ;
 }
 else
 {
  $this->_helper->redirector->goToRouteAndExit(array(), 'page', true);
 }
}

and it is still redirecting.

For the confirm button I have

<a href="next-page"><button>Confirm</button></a>

Could the anchor and button both be calling the next-page?

[final edit] Ok, this IS the problem. It looks like both the button and the anchor are calling the page! So the first time it runs it's ok then it runs again and redirects me :(

Well, there we go. Learn something new everyday.

Thanks for the help all.

A: 

I assume your form submits to /controller/another/ ...

What happens in the //do something block of code there in that action? Since we know that it's not really doing both the "if" and the "else" in the same pass ... it must be doing something in the "if" that you're not showing here that either triggers it to recall the function, _forward()'s, or somehow is getting to this point a 2nd time where it then evaluates the else part of your logic.

Can you post a more complete sample to review?

Coach John
A: 

try replacing your redirect with a die() or $this->_forward( 'page' ) to see if any php warning messages are being hidden by the redirect.

Steve