2012-09-26 117 views
12

Magento 1.7.0.2: 我想獲得一個表單(在後端)上傳文件(圖片)發佈到自身如果不完整,或者adminhtml控制器完成。我的JavaScript驗證工作正常,但是當/如果我的表單已發佈,我將重定向到儀表板。我已經有了一個表格鍵,我的網址是用特殊鍵創建的,但我仍然無法通過POST。誰能幫我?Magento管理員表單重定向到儀表板?

的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('*/*/'); 
    } 
} 
+0

你的$ _POST是空的? – Guerra

+0

我不知道。無論我在頁面重新定向時會丟失什麼。我知道我的控制器永遠不會從這裏獲得一個POST。 –

+0

你可以粘貼你的控制器代碼? – xyz

回答

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的所有數據在你的瀏覽器中。

+0

太棒了!回聲做到了!謝謝一堆。是的,我得迴應HTML的URL。衛生署! –

+0

甜,upvoted,謝謝!我很愚蠢,不包括會話密鑰大聲笑。 – Gogol

+0

不知道需要隱藏的表單字段。謝謝! :) – johnsnails

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. 
} 
} 
+0

之後我已經測試了URL,並且它們指向正確的地方.- http://localhost/magento/index.php/imageadmin/adminhtml_index/index/key/f49c.../但是無論發生什麼,它都不會到達那裏。 –

+0

將擴展類更改爲Mage_Core_Controller_Front_Action – Guerra

+0

現在我有一個非常空白的首頁,而不是完整的後端。這有什麼幫助? –

2

嘗試將此字符串添加到您的表單。

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

這產生對於該請求,其中包含由Magento的使用的form_key一個隱藏的參數。這個form_key用來確保提交的表單來自你的magento-instance(作爲一個安全措施)。如果沒有提供這個form_key,你的表單將無法工作。

+0

解釋說,字符串更好 – Chris

相關問題