OpenCart – Sending Form Data

Html forms are widely spread out throughout the Opencart framework and are relied upon very heavily. Why not learn a little bit of exactly what goes on with the forms and their data? It might help you in your quest to build your own store one day. In today’s world developers rely heavily upon AJAX to send form data because it’s more seamless but whether you are using Ajax or not it’s all still HTTP REQUEST with POST or GET data.

What is a web form?

A web-form, web form or HTML form on a web page allows a user to enter data that is sent to a server for processing. Forms can resemble paper or database forms because web users fill out the forms using check boxes, radio buttons, or text fields. For example, forms can be used to enter shipping or credit card data to order a product, or can be used to retrieve search results from a search engine. The simple definition? It’s a place for a user to type in words, select options, or what have you whereby doing so will create a chain of events in the production of data transfer, all this by pressing a simple SUBMIT button.


In computing, POST is one of many request methods supported by the HTTP protocol used by the World Wide Web. The POST request method is designed to request that a web server accept the data enclosed in the request message’s body for storage.[1] It is often used when uploading a file or submitting a completed web form.

In contrast, the HTTP GET request method is designed to retrieve information from the server. As part of a GET request, some data can be passed within the URI’s query string, specifying for example search terms, date ranges, or other information that defines the query. As part of a POST request, an arbitrary amount of data of any type can be sent to the server in a request message body. A header field in the POST request usually indicates the message body’s Internet media type.

Opencart example FORM

Let’s go to a page on Opencart that has a typical form. Yes, the contact us page! Open the following file: /catalog/view/theme/default/template/information/contact.tpl and find this line:

<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data">

This introduces the form Tag and gives it a URL to direct to once the user clicks submit. Now all we have to do is set up the data that the user will input which will then be sent by HTTP POST and our PHP Server side script. Now take a look at the following code. Within the code you will see various FORM INPUTS. These inputs hold the data that is send with the form. You also MUST close the form with </form> in order for it to work.

<input type="text" name="name" value="<?php echo $name; ?>" />
    <br />
    <?php if ($error_name) { ?>
    <span class="error"><?php echo $error_name; ?></span>
    <?php } ?>
    <br />
    <b><?php echo $entry_email; ?></b><br />
    <input type="text" name="email" value="<?php echo $email; ?>" />
    <br />
    <?php if ($error_email) { ?>
    <span class="error"><?php echo $error_email; ?></span>
    <?php } ?>
    <br />
    <b><?php echo $entry_enquiry; ?></b><br />
    <textarea name="enquiry" cols="40" rows="10" style="width: 99%;"><?php echo $enquiry; ?></textarea>
    <br />
    <?php if ($error_enquiry) { ?>
    <span class="error"><?php echo $error_enquiry; ?></span>
    <?php } ?>
    <br />
    <b><?php echo $entry_captcha; ?></b><br />
    <input type="text" name="captcha" value="<?php echo $captcha; ?>" />
    <br />
    <img src="index.php?route=information/contact/captcha" alt="" />
    <?php if ($error_captcha) { ?>
    <span class="error"><?php echo $error_captcha; ?></span>
    <?php } ?>
    <div class="buttons">

    <div class="right"><input type="submit" value="<?php echo $button_continue; ?>" class="button" /></div>

Now let’s look at the PHP that handles this form data. Open /catalog/controller/information.php and look at this code:

First let’s validate all form fields:

private function validate() {
    	if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
      		$this->error['name'] = $this->language->get('error_name');

    	if (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
      		$this->error['email'] = $this->language->get('error_email');

    	if ((utf8_strlen($this->request->post['enquiry']) < 10) || (utf8_strlen($this->request->post['enquiry']) > 3000)) {
      		$this->error['enquiry'] = $this->language->get('error_enquiry');

    	if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
      		$this->error['captcha'] = $this->language->get('error_captcha');

		if (!$this->error) {
	  		return true;
		} else {
	  		return false;

Now, in the public function index() let’s gather all of the POST FIELD DATA and send it via email to an Admin like this:

if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$mail = new Mail();
			$mail->protocol = $this->config->get('config_mail_protocol');
			$mail->parameter = $this->config->get('config_mail_parameter');
			$mail->hostname = $this->config->get('config_smtp_host');
			$mail->username = $this->config->get('config_smtp_username');
			$mail->password = $this->config->get('config_smtp_password');
			$mail->port = $this->config->get('config_smtp_port');
			$mail->timeout = $this->config->get('config_smtp_timeout');				
	  		$mail->setSubject(html_entity_decode(sprintf($this->language->get('email_subject'), $this->request->post['name']), ENT_QUOTES, 'UTF-8'));
	  		$mail->setText(strip_tags(html_entity_decode($this->request->post['enquiry'], ENT_QUOTES, 'UTF-8')));


Looking for quality OpenCart Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *