Karimx
Karimx

Reputation: 83

controller not displaying a message in phalcon php

I’m trying to make a simple example in Phalcon PHP framework, so i have a view which contain two fields name and email and a submit button. When i click in this button a function of a controller is called to store the name and the email in the DB. This action goes well the problem is I’m trying to display a message after the action ends but i still have the view that contain the form (name, email). Here's my code.

My checkout controller.

<?php

class CheckoutController extends \Phalcon\Mvc\Controller
{

	public function indexAction()
    {
       

    }

    public function registerAction()
    {
        $email = new Emails();

        //Stocker l'email et vérifier les erreurs
        $success = $email->save($this->request->getPost(), array('name', 'email'));

        if ($success) {
            echo "Thanks for shopping with us!";
        } else {
            echo "Sorry:";
            foreach ($user->getMessages() as $message) {
                echo $message->getMessage(), "<br/>";
            }
        }

    }

}

The view

<!DOCTYPE html>
<html>
<head>
	<title>Yuzu Test</title>
</head>
<body>
<?php use Phalcon\Tag; ?>

<h2>Checkout</h2>

<?php echo Tag::form("checkout/register"); ?>

 <td>
 	<tr>
	    <label for="name">Name: </label>
	    <?php echo Tag::textField("name") ?>
 	</tr>
 </td>

 <td>
 	<tr>
	    <label for="name">E-Mail: </label>
	    <?php echo Tag::textField("email") ?>
 	</tr>
 </td>

 <td>
 	<tr>
	     <?php echo Tag::submitButton("Checkout") ?>
 	</tr>
 </td>

</form>

</body>
</html>

Upvotes: 3

Views: 179

Answers (3)

Eric
Eric

Reputation: 43

You can use Flash Messages, so you don't have to break the application flow. Regards

Upvotes: 2

Zahid Khowaja
Zahid Khowaja

Reputation: 66

I think you have to write following line in the end of your controller function registerAction

$this->view->disable();

Upvotes: 0

yergo
yergo

Reputation: 4980

echo() during controller code won't (shouldn't) work unless you turn off your views, because its buffered and cleared after dispatching.

If you want to be sure it's happening this way, just add die() at the end of registerAction() method.

If you create separate view for registerAction(), you can use there variables you declare with $this->view->message = ... or $this->view->setVar('message', ...) in controller method. Than, in view file you can reuse them by <?php echo $this->view->message; ?> or <? echo $message; ?>.

Upvotes: 1

Related Questions