Jump to content

[SOLVED] Module function checking for submit


banditbirds

Recommended Posts

Hi everyone,

 

I'm trying to build a new module. And I have a function which I want to check: when a submit was performed for the id 'submit_requestform':

public function getContentPage()
  {
    $message = '';

    if (Tools::isSubmit('submit_requestform'))
    {
      $message = $this->_saveContent();
      $message = $this->displayConfirmation($this->l('msg in response'));
    }
    $this->_displayContent($message);
  }  

This submit is on display.tpl in modules/blockquote/views/templates/front/

<fieldset>
  <legend>Importing from framework</legend>
  <form method="post">    
    <p>
      <label>Click this to import</label>
      <input id="submit_requestform" name="submit_requestform" type="submit" value="Save" class="button" />
    </p>
  </form>
</fieldset>

But when I submit this on the page, it doesn't trigger the if statement in my function.

 

Can anyone advise what I'm doing wrong??

 

Thanks guys!

 

Simon

Edited by banditbirds (see edit history)
Link to comment
Share on other sites

public function getContentPage()
  {
    $message = '';

    if (Tools::isSubmit('submit_requestform'))
    {
      $message = $this->_saveContent();
      $message = $this->displayConfirmation($this->l('msg in response'));
    }
    $this->_displayContent($message);
  }  

you use this function somewhere? if not, prestashop will not execute it automatically. you have to call it somewhere

Link to comment
Share on other sites

Thanks for the reply Vekia,

 

Ideally I shouldn't need to have that function. From reading the DB Best Practices...my understanding was that I could have:

<form method="post" action="insert.php">  

And then have the DB INSERT function in insert.php

<?php 

if(isset($_POST['submit']))
{
  $name = Tools::getValue('request_details_form');

  $variablename1 = " INSERT INTO `ps_custom_order` (`request_details`)  VALUES('{$name}');";

  if (!Db::getInstance()->execute($variablename1))
      die('Error etc.)';
}
?>

But this doesn't work!!!

 

If I have the database INSERT in my module php file, it works.

 

But the form in the tpl in modules/blockquote/views/templates/front/ doesn't run the insert.php (or at least doesn't run the code in it).

 

 

So I tried making a function in the module php file (in my first post) as a workaround - I don't know how to call this function when I submit my form??

 

Which is the best way of performing this task?

 

I've spent weeks on this now, and no one seems to be able to help. I really appreciate your help.

 

Thanks!

 

Simon

Link to comment
Share on other sites

 

 

So I tried making a function in the module php file (in my first post) as a workaround - I don't know how to call this function when I submit my form??

it's not a workaround, it's the only proper way :P

 

to call this function when you're submitting form - just place it in some hook.

there, where you displaying tpl file.

 

what hook you use to dispaly module .tpl contents?

  • Like 1
Link to comment
Share on other sites

Ok great!

 

Here is my function for the hook on the left column which displays my blockquote.tpl which links to my display.php in modules/blockquote/views/templates/front/:

public function hookDisplayLeftColumn($params)
  {
     $this->context->smarty->assign(
      array(
          'my_module_name' => Configuration::get('MYMODULE_NAME'),
          'my_module_link' => $this->context->link->getModuleLink('blockquote', 'display')
      )
    );
      return $this->display(__FILE__, 'blockquote.tpl');
  }

I then have my display.php in the controller /modules/blockquote/controllers/front/ to display the display.tpl:

 

<?php
class blockquotedisplayModuleFrontController extends ModuleFrontController
{
  public function initContent()
  {
    parent::initContent();
    $this->setTemplate('display.tpl');
  }
}
?>

So I should create a function in here ^^^^ ?

Link to comment
Share on other sites

What is your Prestashop version ?

 

If your HTML form is reside on display.tpl and you have public function postProcess() in

/modules/blockquote/controllers/front/display.php file to process the form,

then your form action URL should be refering to this controller file
 

display.php

<?php
class blockquotedisplayModuleFrontController extends ModuleFrontController
{

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

   public function initContent()
   {
      // Prepare Vars

      $this->setTemplate('display.tpl');
   }
}

display.tpl

<form action="{$link->getModuleLink('blockquote', 'display')|escape:'html'}" method="post">
      <!-- Your HTML Form Content -->
      <input type="submit" name="submit_requestform" value="Submit" />
</form>
  • Like 3
Link to comment
Share on other sites

My version is:1.6.0.5

 

I now have:   /modules/blockquote/controllers/front/display.php

<?php
class blockquotedisplayModuleFrontController extends ModuleFrontController
{
   public function postProcess()
   {
      if (Tools::isSubmit('submit'))
      {
       $varRequestDetails = Tools::getValue('formRequestDetails');

       $sql = " INSERT INTO `ps_custom_order` (`request_details`)  VALUES('{$varRequestDetails}');";

       if (!Db::getInstance()->execute($sql))
         die('Error etc.)';
      }
   }

  public function initContent()
  {
    parent::initContent();
    $this->setTemplate('display.tpl');
  }
  }
?>

And:   /modules/blockquote/views/templates/front/display.tpl

<fieldset>
 <form action="{$link->getModuleLink('blockquote', 'display')|escape:'html'}" method="post">  
    <p>
      <legend>Request Details</legend>
      <input type="text" name="formRequestDetails" maxlength="50">
      <label>Click this to Submit</label>
      <input id="submit" name="submit" type="submit" value="Submit" class="button" />
    </p>
  </form>
</fieldset>

But I'm just getting a blank page at the address: http://www.banditbirds.co.uk/index.php?fc=module&module=blockquote&controller=display

 

Thanks!

 

Simon

Link to comment
Share on other sites

  • 2 years later...

Hello. I am working on a module in prestashop 1.6, and was wondering if there is any way when the user submits the form (press the save button) to pop up a message in javascript where it asks if it really wants to save, as it exists to delete I would like to do this for to save. Thank you for some help. I apologize for my English. Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...