简体   繁体   中英

Zend FrameWork - Form input from Layout to Controller

I have a small form, where user's can Subscribe to my newsletter. How can i pass the email address from my Layout to my Controller?

My Layout name is Footer.phtml, here's the code:

<div id="subscribe">

<form name="newsletterRegister" method="post" action="">
<span>Subscribe to the Newsletter</span>
<input class="subscribeNewsletter" name="email" type="text">
<input id="subscribe_ok" type="image" src="/www/assets/newImages/footer/Ok.png" value="">
</form>

</div>

I have a controller called NewsletterController.php

I'm kinda lost with Zend Framework, can anyone help me figuring out what i have to do??

Well change this

<form name="newsletterRegister" method="post" action="">

To this

<form name="newsletterRegister" method="post" action="NewsletterController/YOURACTION">

And in your controller just get the data like this

$request = $this->getRequest();
$request->getPost()

If the action of your form is empty, it will post to itself.

But maybe you dont want to check on every page if the newsletter is send.

Try using a Controller Plugin , check the request object for the input field, name it unique like email_newsletter , if is not empty, do your logic.

File: application/plugins/Newsletter.php

class Application_Plugin_Newsletter extends Zend_Controller_Plugin_Abstract {

    //before dispatching starts
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $email = $request->getParam('email_newsletter',NULL);
        if(!is_null($mail)) {
            //check if is valid
            if(Zend_Validate::is($email,'')) {
                //do your logic
            }
            else {
                //set some error messages
                //maybe use helper flashMessenger
            }
        }
    }
}

File: Bootrap.php

protected function _initPlugins() {
    $this->bootstrap('frontController');

    //Get FrontController Instance
    $frontController = $this->getResource('frontController');

    $frontController->registerPlugin(new Application_Plugin_Newsletter());

    return $frontController;
}

OR

Set a form action like '/newsletter/subscribe'. Then in controller 'newsletter' action 'subscribe' check the form and redirect to the sending page.

Maybe you should store sth like a last page visited to the session, or add a hidden input to that newsletter form, representing the current page you want to redirect to after the newsletter subscription is done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM