Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

controller not displaying a message in phalcon php

Tags:

php

phalcon

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>
like image 865
Karimx Avatar asked May 29 '26 06:05

Karimx


2 Answers

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

like image 122
Eric Avatar answered May 31 '26 18:05

Eric


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; ?>.

like image 35
yergo Avatar answered May 31 '26 19:05

yergo