Jump to content

Two forms trough _postProcess()


PatrickJose
 Share

Recommended Posts

I made a simple module that stores some information in another table on database.

I have two forms, so I have two submit buttons the problem is:

When I submit form_1 the form_2 will submitted too and inserting empty records in my table.

In the private function _postProcess() i have:

form_1

$field1 = Tools::getValue('field1');
$field2 = Tools::getValue('field2');
$field3 = Tools::getValue('field3');

Db::getInstance()->Execute('
INSERT INTO '._DB_PREFIX_.'test (col1, col2, col3)
VALUES ("'.$field1.'","'.$field2.'","'.$field3.'")');



form_2

$get = Tools::getValue('get');

Db::getInstance()->Execute('
INSERT INTO '._DB_PREFIX_.'test2 (col)
VALUES ("'.$get.'")');



With these will work, but if I submit only one form the another will sent empty to table.

I tried to get with $_POST inside private function _postProcess()

private function _postProcess()
{
if (isset($_POST['submitA']))
{
$field1 = Tools::getValue('field1');
$field2 = Tools::getValue('field2');
$field3 = Tools::getValue('field3');

Db::getInstance()->Execute('
INSERT INTO '._DB_PREFIX_.'test (col1, col2, col3)
VALUES ("'.$field1.'","'.$field2.'","'.$field3.'")');
} elseif (isset($_POST['submitB']))
{
$get = Tools::getValue('get');

Db::getInstance()->Execute('
INSERT INTO '._DB_PREFIX_.'test2 (col)
VALUES ("'.$get.'")');
}
}


In form_1 my submit button have name and id = submitA

In form_2 my submit button have name and id = submitB

Looking another modules I found the function Tools::isSubmit(), i tried with the function but no success

private function _postProcess()
{
if (Tools::isSubmit('submitA'))
{
$field1 = Tools::getValue('field1');
$field2 = Tools::getValue('field2');
$field3 = Tools::getValue('field3');

Db::getInstance()->Execute('
INSERT INTO '._DB_PREFIX_.'test (col1, col2, col3)
VALUES ("'.$field1.'","'.$field2.'","'.$field3.'")');
} elseif (Tools::isSubmit('submitB'))
{
$get = Tools::getValue('get');

Db::getInstance()->Execute('
INSERT INTO '._DB_PREFIX_.'test2 (col)
VALUES ("'.$get.'")');
}
}



Anyone have an idea?

Regards

Share this post


Link to post
Share on other sites

It's hard to understand what is wrong without seeing the html.

The two forms should be separate, and if they are, the values from only one of them will be passed to the action script each time.

Share this post


Link to post
Share on other sites

Yes, my two fords are separated... but they have the same action="'.$_SERVER['REQUEST_URI'].'".

I have:

form 1

<form>

     content

<form>



form 2

<form>

     content

<form>



Just now I realized that both forms don't have name or id.

Because the postProcess need to know who is calling the function... I'll try to upload my hole block.

Edit:

Solved. I forgot to set both submit buttons on function getContent()

if ((Tools::isSubmit('submitA')) OR (Tools::isSubmit('submitB')))
{            
   $this->_postValidation();
   if (!sizeof($this->_postErrors))
       $this->_postProcess();
   else
       foreach ($this->_postErrors AS $err)
           $this->_html .= ''. $err .'';
}



Now I get one problem, when I fill one of three text input and press submit it gives me an error, ok, but what I filled comes empty on text input...

What's the difference between:

$myx = Tools::getValue('myx');



and

$myz = $_POST['myz'];

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More