【问题标题】:Prestashop Handle post data in custom CMS formPrestashop 以自定义 CMS 表单处理发布数据
【发布时间】:2023-04-07 03:18:02
【问题描述】:

我开发了一个显示表单的 prestashop 模块,现在我想使用 POST 数据将我的数据存储在数据库中。

按照一些教程,我可以显示表单并加载一些 js 文件,但我的问题是两个:

  • 我的表单的操作参数是什么?

  • 如何处理 post 参数,在哪里处理?

我的模块的结构是这样的 - 根是 /modules/mymodule/ dir:

  • mymodule.php

  • /views/templates/hook/mymodule.tpl

  • /views/js/front.js

我要插入控制器吗?

谢谢。

编辑-添加一些代码

我的模块.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');
    }
}

我的模块.tpl

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

display.php(应该是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");


      }
    }

}

就是这样……

【问题讨论】:

  • 这个表格是什么?模块配置?首页?管理页面?如果是首页或管理页面,是的,您需要一个控制器。您还需要提供更多信息和一些代码,以便我们进一步帮助您。
  • @TheDrot 我已经编辑了添加我的代码的帖子。应该是前端控制器。我的范围是添加一个包含一些字段和逻辑的表单,并将数据存储在数据库中。我省略了一些无用的代码,例如配置。
  • 控制器的类声明错误。查看here 进行正确声明。
  • @TheDrot 解决了调用流程。不是我可以继续我的发展!谢谢!

标签: php prestashop prestashop-1.6


【解决方案1】:

请在下面找到您问题的答案:

  • 我的表单的操作参数是什么?

表单的操作参数将是

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

您需要在 getContent() 函数中从控制器 (mymodule.php) 将其分配给 smarty,然后您可以将其用作 TPL 文件中的操作。

  • 如何处理 post 参数,在哪里处理??

您可以使用以下代码在 mymodule.php - getContent() 函数中获取您的帖子参数的值:

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

【讨论】:

    【解决方案2】:

    要从表单中获取发布的数据,您必须使用

    Tools::getValue('PARAM_NAME');
    

    要将数据插入数据库,您应该使用

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

    要从参数的数据库中获取值,请使用

    Configuration::get('PARAM_NAME');
    

    【讨论】:

      【解决方案3】:

      您不需要添加前端控制器。您只需将表单提交到实际的 CMS URL 并在 hookcustomCMS($params) 函数中操作 POST 数据。

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

      【讨论】:

      • 这似乎有效,但我在之前的评论中找到了@TheDort 建议的解决方案。
      【解决方案4】:

      如果只获取具体值(POST+GET),可以使用:

      Tools::getValue('param');
      

      如果您想从 POST + GET 中获取所有值,请使用:

      Tools::getAllValues();
      

      也参考[prestashop_folder]/class/Tools.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多