A free and open-source book on ZF3 for beginners


7.5. Retrieving Form Data in a Controller's Action

The website user typically works with the form in the following order:

Typically these two web pages are handled by the same controller action.

In the following example, we will show how you can create a controller action for displaying the feedback form and retrieving the data submitted by the user. To start, add the contact-us.phtml view template in the application/index/ directory under the module's view/ directory (see figure 7.6 for example).

Figure 7.6. Creating the contact-us.phtml file Figure 7.6. Creating the contact-us.phtml file

Put the HTML markup code of the feedback form from the previous section into the view template file.

Then, add the contactUsAction() action method to the IndexController class. In the action method, we want to extract raw data from the feedback form submitted by the site user:

<?php
namespace Application\Controller;

// ...

class IndexController extends AbstractActionController 
{
  // This action displays the feedback form
  public function contactUsAction() 
  {
    // Check if user has submitted the form
    if($this->getRequest()->isPost()) {
      
	  // Retrieve form data from POST variables
	  $data = $this->params()->fromPost();     
	  
	  // ... Do something with the data ...
	  var_dump($data);	  
    } 
        
    // Pass form variable to view
    return new ViewModel([
          'form' => $form
       ]);
  }
}

In the code above, we define the contactUsAction() action method in the IndexController class (line 9).

Then, in line 12, we check whether the request is a POST request (checking the starting line of the HTTP request). Typically, the form uses the POST method for submitting the data. For this reason, we can detect if the form is submitted or not by checking the starting line of the HTTP request.

In line 15 we retrieve the raw data submitted by the user. We extract all the POST variables with the help of the Params controller plugin. The data is returned in the form of an array and saved into the $data variable.

Finally, we have to add a literal route to make a short and memorable URL for the Contact Us page. Add the following contactus key to the routing configuration in the module.config.php file:

<?php
return [
  // ...
  'router' => [
    'routes' => [
      // Add the following routing rule for the "Contact Us" page 
      'contactus' => [
        'type' => Literal::class,
          'options' => [
             'route'    => '/contactus',
             'defaults' => [
               'controller' => Controller\IndexController::class,
               'action'     => 'contactUs',
             ],
           ],
         ],
       ],		 
    ],
  ],  
  // ...
];

Now, if you type the "http://localhost/contactus" URL in your web browser's navigation bar, you should see the page as in figure 7.7.

Figure 7.7. Feedback Form Figure 7.7. Feedback Form

Enter an E-mail, subject, and body text and click the Submit button on the form. The data will be sent to the server, and finally extracted in the IndexController::contactUsAction() method.

Below, an example of the $data array (produced with the var_dump() PHP function) is shown. As you can see, the array contains a key for each form field, including the "submit" field.

array (size=4)
    'email' => string 'name@example.com' (length=16)
    'subject' => string 'Happy New Year!' (length=15)
    'body' => string 'Dear Support, I'd like to thank you for the 
              excellent quality of your support service and wish you 
              a Happy New Year!' (length=118)
    'submit' => string 'Submit' (length=6)

Top