Prestashop Handle post data in custom CMS form

坚强是说给别人听的谎言 提交于 2019-12-23 04:21:16

问题


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:

  • What will be the action parameter of my form?

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

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

  • mymodule.php

  • /views/templates/hook/mymodule.tpl

  • /views/js/front.js

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...


回答1:


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');



回答2:


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');



回答3:


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');
    }



回答4:


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



来源:https://stackoverflow.com/questions/40762425/prestashop-handle-post-data-in-custom-cms-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!