Jack
Jack

Reputation: 538

Prestashop Handle post data in custom CMS form

I've developped a prestashop module that display a form, and now I want to use the POST data to store my data in the database.

Following some tutorials I'm able to display the form and load some js file, but my question are two:

The structure of my module is this - root is /modules/mymodule/ dir:

Have i to insert a controller??

Thank you.

EDIT- Add some code

mymodule.php

class MyModule extends Module
{
    public function __construct()
    {
        $this->name = 'mymodule';
        $this->controllers = array( 'display' ); // <- my controller name

        parent::__construct();
    }

    public function install()
    {
       if (Shop::isFeatureActive())
         Shop::setContext(Shop::CONTEXT_ALL);

       if (!parent::install() ||
         !$this->registerHook('customCMS') ||
         !$this->registerHook('header')
       )
          return false;

       return true;
     }

    public function hookcustomCMS($params)
    {
        if (Tools::getValue('id_cms') != 7)
            return;

      $this->context->smarty->assign(
          array(
              'form_link' => $this->context->link->getModuleLink('mymodule', 'display')
          )
      );

      return $this->display(__FILE__, 'mymodule.tpl');
    }
}

mymodule.tpl

<form id="myform" action="{$link->getModuleLink('mymodule', 'display')|escape:'html'}" method="post">
<!-- all fields...  + submit button -->
</form>

display.php (this shoul be the controller in mymodule/controllers/front)

<?php
class mymoduledisaplyFrontController extends ModuleFrontController
{


    public function initContent()
    {
        parent::initContent();


        $this->context->controller->addJS($this->module->getLocalPath().'views/js/front.js');
        $this->setTemplate('mymodule.tpl');
    }



    public function postProcess()
    {
      if (Tools::isSubmit('submit_requestform'))
      {
          // form processing

          ppp("OK");


      }
    }

}

That's all...

Upvotes: 0

Views: 6407

Answers (4)

Thanh Vo
Thanh Vo

Reputation: 31

If only get the specific value (POST+GET), you can use:

Tools::getValue('param');

If you want to get all values from POST + GET please use:

Tools::getAllValues();

And also refer from [prestashop_folder]/class/Tools.php

Upvotes: 1

Knowband Plugins
Knowband Plugins

Reputation: 1317

Please find the answers to your questions below:

  • What will be the action parameter of my form?

The action parameter for your form will be

$this->smarty->assign('action', 'index.php?controller=AdminModules&token='.Tools::getAdminTokenLite('AdminModules').'&configure='.$this->name)

You need to assign it to smarty from your controller (mymodule.php) in getContent() function and then you can use it as action in your TPL file.

  • How can I handle the post parameters, and where??

You can get the values of your post parameters in mymodule.php - getContent() function by using the following code:

$post_param = Tools::getValue('name_of_parameter');

Upvotes: 2

PixelWeb
PixelWeb

Reputation: 568

You don't need to add a front controller. You can just submit your form to actual CMS URL and manipulate POST data inside your hookcustomCMS($params) function.

    public function hookcustomCMS($params)
    {
        if (Tools::getValue('id_cms') != 7)
            return;
        if (Tools::isSubmit('submit_requestform'))
        {
              //form proccessing
        }

      $this->context->smarty->assign(
          array(
              'form_link' => $this->context->link->getModuleLink('mymodule', 'display')
          )
      );

      return $this->display(__FILE__, 'mymodule.tpl');
    }

Upvotes: 0

G. Mansour
G. Mansour

Reputation: 696

To get posted data from the form you have to use

Tools::getValue('PARAM_NAME');

And to insert data to Database you should use

Configuration::updateValue('PARAM_NAME', Tools::getValue('PARAM_NAME'));

To get Values from Database of your params use

Configuration::get('PARAM_NAME');

Upvotes: 0

Related Questions