Submit form values using controller and model in magento admin module

17,999

Solution 1

Usually when you are getting redirected to the dashboard because your key is not recognised. I normally would use the following syntax

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

Also, I would be inclined to disable admin keys to verify whether that is your issue.

Next, never, ever use $_POST with Magento, all variables are accessible (cleanly and securely). POST information is accessible globally (not just in a controller), however you can still set variables in your model from the controller like this:

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

Then accessing them from your model with:

$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 fetch a single POST value - but it does not get GET values. getParam will return both GET and POST values alike.

Solution 2

Request params are accessible via the request object, Mage_Core_Controller_Request_Http. Example usage:

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

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

Share:
17,999
Zac
Author by

Zac

Updated on June 15, 2022

Comments

  • Zac
    Zac almost 2 years

    I am building an admin module and need to process a form but am having troubles getting the variables to POST and am just redirected to the dashboard. The form looks like this :

    <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 am doing it that way because the secure keys are turned on for admin. It gives me a url like :

    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 would hope to be able to process the POST variables from my form in here but I can not see what is wrong and I am just sent to dashboard.

    <?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 am using $_POST. I was setting up a simple example to just get the form to post but realizing there are probably many things 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) . ';';
    

    The dream is I could $resource->getConnection('core_write'); to actually insert this into the database but feeling less optimistic with how it is going so far.

  • Zac
    Zac about 12 years
    Thanks, I updated my question a bit to show the crazy way I am trying to get all of these values (from radio buttons) and then INSERT them. So are you saying from the example in my question I would add what you have to the controller, and then in the model have something like $sku=Mage::app()->getRequest()->getParam('color'); ?
  • Zac
    Zac about 12 years
    Hi, sorry I am pretty lost with this stuff. Can you please elaborate some and maybe provide an example? Otherwise it is just so abstract to me right now.
  • Ben Lessani
    Ben Lessani about 12 years
    You use either method I recommended. Also, using $resource->getConnection('core_write'); is not best practice for inserting into the DB - you should really create your own MySQL4 resource in your extension.
  • Ben Lessani
    Ben Lessani about 12 years
    Why would you call $model->save()? Surely using a Model singleton would mitigate that requirement, and the need for the OP to have to write/use a save() or load() method?