如何在主页中创建客户信息表单并在opencart中的数据库中形成数据存储

时间:2016-06-06 07:30:33

标签: forms model-view-controller opencart2.x

我曾尝试创建表单,但数据未保存在数据库表中。

我写了以下代码。

视图/普通/ home.tpl:

<form action="" method="post" enctype="multipart/form-data" class="form-horizontal form" id="contact">
<legend><?php echo $text_form; ?></legend>
          <div class="form-group required">
            <label class="col-sm-3 control-label" for="input_first_name"><?php echo $entry_first_name; ?></label>
            <div class="col-sm-9">
              <input type="text" name="first_name" value="<?php echo $first_name; ?>" id="input_first_name" class="form-control" />
              <?php if ($error_first_name) { ?>
              <div class="text-danger"><?php echo $error_first_name; ?></div>
              <?php } ?>
            </div>
          </div>
<div class="buttons">
          <div class="pull-right">
            <input class="btn btn-primary" type="submit" name="submit" value="<?php echo $button_submit; ?>" />
          </div>
        </div>
</form>

控制器/通用/ home.php

class ControllerCommonHome extends Controller 
{
    public function index() {

        if (isset($this->request->get['route'])) {
            $this->document->addLink(HTTP_SERVER, 'canonical');
        }

        $this->load->language('common/home');

        $this->document->setTitle($this->language->get('heading_title'));

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_account'),
            'href' => $this->url->link('common/home', '', 'SSL')
        );

        $url = '';

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('common/home', $url, 'SSL')
        );

        $data['heading_title'] = $this->language->get('heading_title');

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $data['returns'] = array();



        $data['id'] = $this->language->get('id');
        $data['first_name'] = $this->language->get('first_name');



        $data['button_continue'] = $this->language->get('button_continue');

        $this->load->model('common/home');

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
            $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/home.tpl', $data));
        } else {
            $this->response->setOutput($this->load->view('default/template/common/home.tpl', $data));
        }
    }

    public function add() {
        $this->load->language('common/home');

        $this->load->model('common/home');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $return_id = $this->model_common_home->addCustomerbook($this->request->post);

            // Add to activity log
            $this->load->model('common/home');



            $this->response->redirect($this->url->link('common/home/success', '', 'SSL'));
        }

        $this->document->setTitle($this->language->get('heading_title'));
        $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment.js');
        $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js');
        $this->document->addStyle('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.css');

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_account'),
            'href' => $this->url->link('common/home', '', 'SSL')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('common/home/add', '', 'SSL')
        );

        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_first'] = $this->language->get('text_first');


        $data['entry_first_name'] = $this->language->get('entry_first_name');


        $data['button_submit'] = $this->language->get('button_submit');
        $data['button_back'] = $this->language->get('button_back');

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->error['order_id'])) {
            $data['error_order_id'] = $this->error['order_id'];
        } else {
            $data['error_order_id'] = '';
        }

        if (isset($this->error['first_name'])) {
            $data['error_first_name'] = $this->error['first_name'];
        } else {
            $data['error_first_name'] = '';
        }

        $data['action'] = $this->url->link('common/home/add', '', 'SSL');



        if (isset($this->request->post['firstname'])) {
            $data['firstname'] = $this->request->post['firstname'];
        } elseif (!empty($order_info)) {
            $data['firstname'] = $order_info['firstname'];
        } else {
            $data['firstname'] = $this->customer->getFirstName();
        }

        // Captcha
        if ($this->config->get($this->config->get('config_captcha') . '_status') && in_array('return', (array)$this->config->get('config_captcha_page'))) {
            $data['captcha'] = $this->load->controller('captcha/' . $this->config->get('config_captcha'), $this->error);
        } else {
            $data['captcha'] = '';
        }


        $data['back'] = $this->url->link('common/home', '', 'SSL');

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/return_form.tpl')) {
            $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/account/return_form.tpl', $data));
        } else {
            $this->response->setOutput($this->load->view('default/template/account/return_form.tpl', $data));
        }
    }

    protected function validate() {
      .......
    }

    public function success() {
        $this->load->language('common/home');

        $this->document->setTitle($this->language->get('heading_title'));

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('account/return', '', 'SSL')
        );

        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_message'] = $this->language->get('text_message');

        $data['button_continue'] = $this->language->get('button_continue');

        $data['continue'] = $this->url->link('common/home');

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
            $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/common/success.tpl', $data));
        } else {
            $this->response->setOutput($this->load->view('default/template/common/success.tpl', $data));
        }
    }

}

模型/普通/ home.php

class ModelCommonHome extends Model {
    public function addCustomerbook($data) {

        $this->event->trigger('pre.home.add', $data);

        $this->db->query("INSERT INTO `" . DB_PREFIX . "contact_book` SET contact_id = '" . (int)$data['id'] . "', first_name = '" . $this->db->escape($data['first_name']) . "' ");

        $id = $this->db->getLastId();

        $this->event->trigger('post.home.add', $id);

        return $id;

    }

我不太擅长MVC所以请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

Sujit,您需要检查opencart联系我们表单功能,以便将日期发布到控制器。联系我们表单不会将数据存储到数据库,但您将在控制器文件

上进行以下更改
$this->load->model('catalog/contact');
$this->model_catalog_contact->addContact($this->request->post;  
  

模型/目录/ contact.php

在其上创建addContact($ data)函数,然后根据需要进行插入查询。