13

Magento 1.7.0.2:如果不完整,我正在尝试获取一个表单(在后端)以将文件(图片)上传到自身,或者如果完成,则向 adminhtml 控制器上传。我的 JavaScript 验证运行良好,但是当/如果我的表单已发布时,我将被重定向到仪表板。我已经包含了一个表单密钥,并且我的 url 是使用特殊密钥创建的,但我仍然无法通过 POST。谁能帮我?

phtml 模板文件:

<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>

控制器:只有 insertAction() 函数适用于此表单。剩下的就是处理任何已经上传的图像的 gridview 东西。

<?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('*/*/');
    }
}
4

3 回答 3

23

您需要检查几件事:

  1. 你在echo这里错过了:

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

    应该

    action="<?php echo Mage::helper("adminhtml")->getUrl("*/*/insert")?>"
    
  2. 确保您只使用普通的 PHP 标记 ( <?php ?>)。短标签已被证明是一种不好的做法,所以改变

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

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

除此之外,请确保使用浏览器中的浏览源功能将所有数据正确填充到 HTML 中。

于 2012-09-27T09:27:43.437 回答
2

尝试将此字符串添加到您的表单中。

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

这会为请求创建一个隐藏参数,其中包含 Magento 使用的 form_key。此 form_key 用于确保提交的表单来自您的 magento 实例(作为安全措施)。如果不提供此 form_key,您的表单将无法工作。

于 2015-10-04T07:52:55.347 回答
0

埃里克,你的 .phtml 看起来不错......现在你需要看看你的表单的操作 url 是否指向正确的位置,然后像这样做你的控制器:

class controller_name extends Mage_Core_Controller_Front_Action{ // must extends this one for frontend controllers and Mage_Adminhtml_Controller_Action to backend controller.
public function insertAction(){
    $_POST['elementName']; //to this to get post information. 
    $this->getRequest()->getPost('elementName'); //or this way.
}
}
于 2012-09-26T20:03:19.350 回答