Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Admin form redirects to dashboard on ?post?

Magento 1.7.0.2: I'm trying to get a form (in the backend) to upload a file(picture) to Post to itself if incomplete, or the adminhtml controller if complete. My JavaScript validation is working well, but when/if my form is POSTed I'm redirected to the dashboard. I've got a form key included and my url's are created with the special key, but still I can't get a POST through. Can anyone help me?

The phtml template file:

<script type="text/javascript">
function postSelf(){
    form=document.getElementById('imgSel');
    form.action='<?php Mage::helper("adminhtml")->getUrl("*/*/")?>';
    form.submit();
}    
function validateForm(){

    var name=document.forms["imgSel"]["iName"].value;
    var file=document.forms["imgSel"]["file_upload"].value;
    if (!name){
        alert("You must have an Image Name!");
        postSelf();
    }
    else if (!file){
        alert("You must have a File to upload");
        postSelf();
    }
    else{
        form=document.getElementById('imgSel');
        form.submit();
    } 
}  
    </script>

    <?php Mage::log(Mage::helper("adminhtml")->getUrl("*/*/"), null, ‘layout.log’ );?>

<h3 class="icon-head head-adminhtml-imagegrid">Add an Image:</h3>

<form name="imgSel" id="imgSel" action="<?php Mage::helper("adminhtml")->getUrl("*/*/insert")?>" 
      enctype="multipart/form-data" method="POST">
<!--Form key-->
    <input type="hidden" name="form_key" value="<? echo Mage::getSingleton('core/session')->getFormKey(); ?>" />
    <?php Mage::log(Mage::getSingleton('core/session')->getFormKey(), null, ‘layout.log’ );?>

    <label for="iName">Image Name:</label>
    <input type="text" name="iName">

    <label for="style">Associated Style Name:</label>
    <select name="style">
    <?php
    echo '<option value="-1">None</option>';
    $styles = Mage::getModel('cartonplugin/cartonstyle')->getCollection();
    foreach($styles as $style){
        echo '<option value="'.$style->getId().'"';
        echo '>'.$style->getData('style_name').'</option> ';
    }
    echo '</select><br />';
    ?>
    <input type="hidden" name="MAX_FILE_SIZE" value="40" />
    Upload Image: <input type="file" name="file_upload" />
    <br>
    <!--<input type="submit" value="submit">-->
    <button onClick="validateForm()" class="UploadButton" >Upload</button>
</form>

Controller: Only the insertAction() function is for this form. The rest is gridview stuff for dealing with any already-uploaded images.

<?php
class Nationwide_Newcart_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action
{
    protected function _initAction()
    {
        $this->loadLayout()->_setActiveMenu('igrid/set_time7')
                ->_addBreadcrumb('image Manager','image Manager');
       return $this;
     }

    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();

        //var_dump(Mage::getSingleton('core/layout')->getUpdate()->getHandles());
     }

      public function newAction()
    {
        $this->_forward('edit');
    }

    public function editAction()
    {
        $stId = $this->getRequest()->getParam('id');
        $model = Mage::getModel('newcart/imagemodel')->load($stId);
        if ($model->getId() || $stId == 0)
           {
             Mage::register('image_data', $model);
             $this->loadLayout();
             $this->_setActiveMenu('igrid/set_time7');
             $this->_addBreadcrumb('image Manager', 'image Manager');
             $this->_addBreadcrumb('Image Description', 'Image Description');
             $this->getLayout()->getBlock('head')
                  ->setCanLoadExtJs(true);
             $this->_addContent($this->getLayout()
                  ->createBlock('newcart/adminhtml_imagegrid_edit'))
                  ->_addLeft($this->getLayout()
                  ->createBlock('newcart/adminhtml_imagegrid_edit_tabs')
              );
             $this->renderLayout();
           }
           else
           {
                 Mage::getSingleton('adminhtml/session')
                       ->addError('That Image does not exist');
                 $this->_redirect('*/*/');
            }
       }

    public function saveAction()
    {
         if ($this->getRequest()->getPost())
         {
           try {
                $postData = $this->getRequest()->getPost();
                $model = Mage::getModel('');
                //Mage::log($this->getRequest()->getParam('id'), null, ‘layout.log’ );
                if( $this->getRequest()->getParam('id') <= 0 )
                    $model->setCreatedTime(
                        Mage::getSingleton('core/date')
                                ->gmtDate()
                    );
                $model
                    //->addData($postData) //DO NOT! Includes a form key!
                    ->setUpdateTime(
                            Mage::getSingleton('core/date')
                            ->gmtDate())
                    ->setId($this->getRequest()->getParam('id'));
                $model->setData('image_name', $postData['image_name']);
                $model->setData('style_name', $postData['style_name']);
                $model->save();

                Mage::getSingleton('adminhtml/session')
                            ->addSuccess('successfully saved');
                Mage::getSingleton('adminhtml/session')
                                ->settestData(false);
                $this->_redirect('*/*/');
                return;
          } catch (Exception $e){
                Mage::getSingleton('adminhtml/session')
                                  ->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')
                 ->settestData($this->getRequest()
                                    ->getPost()
                );
                $this->_redirect('*/*/edit',
                            array('id' => $this->getRequest()
                                                ->getParam('id')));
                return;
                }
              }
              $this->_redirect('*/*/');
            }

    public function deleteAction()
    {
        if($this->getRequest()->getParam('id') > 0)
        {
            try
            {
                $model = Mage::getModel('newcart/imagemodel');
                $model->setId($this->getRequest()
                                    ->getParam('id'))
                            ->delete();
                Mage::getSingleton('adminhtml/session')
                            ->addSuccess('successfully deleted');
                $this->_redirect('*/*/');
                }
                catch (Exception $e)
                {
                        Mage::getSingleton('adminhtml/session')
                            ->addError($e->getMessage());
                        $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                }
         }
            $this->_redirect('*/*/');
    }

    public function insertAction(){
        $postData = $this->getRequest()->getPost();
        Mage::log($postData, null, ‘layout.log’ );

        //post checking
        if(empty($postData)){

        }

        $this->_redirect('*/*/');
    }
}
like image 824
Eric Bagwell Avatar asked Sep 26 '12 14:09

Eric Bagwell


2 Answers

There are few things you need to check:

  1. You have echo missing here:

    action="<?php Mage::helper("adminhtml")->getUrl("*/*/insert")?>"
    

    Should be

    action="<?php echo Mage::helper("adminhtml")->getUrl("*/*/insert")?>"
    
  2. Make sure you're using only normal PHP tags (<?php ?>). Short tags have proven to be a bad practice, so change

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

    to

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

Along with that make sure you have all data correctly populated in your HTML using browse source feature in your browser.

like image 159
Slayer Birden Avatar answered Nov 17 '22 11:11

Slayer Birden


Try to add this string to your form.

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

This creates a hidden parameter for the request, which contains the form_key used by Magento. This form_key is used to make sure the submitted form originated from your magento-instance (as a security measure). Without supplying this form_key, your form will not work.

like image 2
karick Avatar answered Nov 17 '22 10:11

karick