Enter form values ​​using the controller and model in the magento admin module

I am creating an admin module and need to process the form, but I'm having trouble getting POST variables, and I'm just redirected to the control panel. The form is as follows:

<form name="notes" action="<?php echo Mage::helper("adminhtml")->getUrl("foo/index/processnotes/");?>" method="post"> <input type="hidden" name="another_form_key" value="<? echo $this->getFormKey(); ?>" /> 

I do this because secure keys are enabled for the administrator. This gives me the url:

 foo/index/processnotes/key/4745f5fbb9c168778958d5d4a4c2c0ef/ 

In the controller, I have:

 public function processnotesAction(){ $model = Mage::getModel('foo/process'); // I am not sure how I am supposed to send $_POST values here } 

and in Package/foo/Model/Process.php

I hope that I can process the POST variables from my form here, but I don’t see what’s wrong and I’m just sent to the toolbar.

 <?php class Package_foo_Model_Process extends Mage_Core_Model_Abstract { public function noteProcess() { $test = $_POST['myValue']; echo $test; } } 

Update

After reading the answers, I wanted to add a bit more of what my real code is doing and how I use $ _POST. I created a simple example to just get a form for publishing, but realizing that there are probably a lot of things that I am doing wrong in Magento.

  $query="INSERT INTO `notes_value` ( `color`, `the_id`, `the_value` ) VALUES "; $sku = $_POST['color']; array_pop($_POST); foreach ($_POST AS $key => $value) { $values[] = "('$sku', '$id', '$value')"; } $query .= implode(', ', $values) . ';'; 

Dream: I could $resource->getConnection('core_write'); actually insert this into the database, but felt less optimistic about how this happens.

+6
source share
3 answers

Usually when you are redirected to the control panel because your key is not recognized. I usually used the following syntax

  <input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" /> 

Also, I would tend to disable admin keys to check if this is your problem.

Then never, never use $_POST with Magento, all variables are available (clean and reliable). POST information is available globally (not only in the controller), however you can still set the variables in your model from the controller as follows:

 public function processnotesAction(){ $model = Mage::getSingleton('foo/process'); $model->setData('postdata', $this->getRequest()->getPost() ); } 

Then access them from your model using:

 $this->getData(); $this->getMyPostedField(); 

Or you could just use this in your model

 Mage::app()->getRequest()->getPost(); Mage::app()->getRequest()->getParam('myargument'); 

getPost can also take a parameter to get a single POST value, but it does not receive GET values. getParam will return both GET and POST values.

+9
source

Request parameters are available through the request object, Mage_Core_Controller_Request_Http . Usage example:

 $request = $this->getRequest(); $params = $request->getParams(); $someParam = $request->getParam('param_key'); // or $params = $request->getPost(); 

Then you need to set the data on your model and call $model->save() .

+3
source

If your controller extends the ActionController, then the preDispatch () function is responsible for redirecting you to the control panel (and checking for a valid form key).

I suggest you insert Mage :: log debug info into the PreDispatch ActionController function, which can help determine what is happening.

I was going to post a comment, but do not have a high enough reputation.

(maybe update the message or reply to the answer that helped you to find out how you solved it (or not))

0
source

Source: https://habr.com/ru/post/910842/


All Articles