我试图获得一个表单(在后端)上传一个文件(图片)Post到自己,如果不完整,或adminHTML控制器如果完成.我的 @L_301_0@验证工作正常,但是当/如果我的表单被POST,我被重定向到仪表板.我有一个表单键,我的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 >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(),‘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()" >Upload</button></form>
控制器:只有insertAction()函数用于此表单.其余的是用于处理任何已经上传的图像的grIDvIEw的东西.
<?PHPclass 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'),‘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,‘layout.log’ ); //post checking if(empty($postData)){ } $this->_redirect('*/*/'); }}解决方法 你需要检查几件事情:
>你在这里有回音:
action="<?PHP Mage::helper("adminHTML")->getUrl("*/*/insert")?>"
应该
action="<?PHP echo Mage::helper("adminHTML")->getUrl("*/*/insert")?>"
>确保您只使用正常的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中正确填充所有数据.
总结以上是内存溢出为你收集整理的表单 – Magento管理表单重定向到仪表板上?全部内容,希望文章能够帮你解决表单 – Magento管理表单重定向到仪表板上?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)