You start by defining a route for your module. This is done in <module_directory>/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="timestwo" frontName="timestwo">
<module name="Your_Module"/>
</route>
</router>
</config>
Then make the 'Index' controller for this route under <module_directory>/Controller/Index.php
<?php
namespace Your\Module\Controller;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
protected $resultPageFactory;
public function __construct(Context $context, PageFactory $pageFactory)
{
$this->resultPageFactory = $pageFactory;
parent::__construct($context);
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
return $resultPage;
}
}
Make the layout file for this page under <module_directory>/view/frontend/layout/timestwo_index.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>Form</title>
</head>
<body>
<referenceContainer name="content">
<block name="form" class="Magento\Framework\View\Element\Template" template="Your_Module::form.phtml"/>
</referenceContainer>
</body>
</page>
Now make the template <module_directory>/view/frontend/templates/form.phtml
<form class="form" action="result" method="post">
<fieldset class="fieldset">
<legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Test Form') ?></span></legend><br>
<div class="field required">
<label for="number" class="label">
<span><?php /* @escapeNotVerified */ echo __('Input') ?></span>
</label>
<div class="control">
<input type="text" id="number"
name="number"
class="input-text">
</div>
</div>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit" class="action submit primary" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>">
<span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
</button>
</div>
</div>
</form>
Now make the controller for the result page: <module_directory>/Controller/Result.php
<?php
namespace Your\Module\Controller;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Element\Messages;
use Magento\Framework\View\Result\PageFactory;
class Result extends Action
{
/** @var PageFactory $resultPageFactory */
protected $resultPageFactory;
/**
* Result constructor.
* @param Context $context
* @param PageFactory $pageFactory
*/
public function __construct(Context $context, PageFactory $pageFactory)
{
$this->resultPageFactory = $pageFactory;
parent::__construct($context);
}
/**
* The controller action
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
$number = $this->getRequest()->getParam('number');
$resultPage = $this->resultPageFactory->create();
/** @var Messages $messageBlock */
$messageBlock = $resultPage->getLayout()->createBlock(
'Magento\Framework\View\Element\Messages',
'answer'
);
if (is_numeric($number)) {
$messageBlock->addSuccess($number . ' times 2 is ' . ($number * 2));
}else{
$messageBlock->addError('You didn\'t enter a number!');
}
$resultPage->getLayout()->setChild(
'content',
$messageBlock->getNameInLayout(),
'answer_alias'
);
return $resultPage;
}
}
Finally, make the layout xml for the result page in <module_directory>/view/frontend/layout/timestwo_result.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>Result</title>
</head>
</page>
Run php bin/magento setup:upgrade
to enable your module, if you haven't already.
Now clear the cache.
Go to <magento_location>/timestwo
in browser to see the form.
Create a controller file SfuploadController.php under MageSf/OrderCustom/controllers/Adminhtml
<?php
class MageSf_OrderCustom_Adminhtml_SfuploadController extends Mage_Adminhtml_Controller_Action {
public function uploadAction() {
if (isset($_FILES['docname']['name']) && $_FILES['docname']['name'] != '') {
try {
$uploader = new Varien_File_Uploader('docname');
$uploader->setAllowedExtensions(array('doc','pdf','txt','docx'));
$uploader->setAllowCreateFolders(true);
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$newDir = "customer_documents";
$newdirPath = Mage::getBaseDir('media') . DS . "customer_documents";
if (!file_exists($newdirPath)) {
mkdir($newdirPath, 0777);
}
$path = Mage::getBaseDir('media') . DS . $newDir . DS;
$resizedPath = Mage::getBaseDir('media') . DS . $newDir;
$uploader->save($path, $_FILES['docname']['name']);
$filename = $uploader->getUploadedFileName(); // Uploaded File name
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('core')->__('File uploaded successfully.'));
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('core')->__('Unable to find item to save'));
}
}
$this->_redirectReferer();
}
}
Update your config.xml and add below code
<admin>
<routers>
<adminhtml>
<args>
<modules>
<MageSf_OrderCustom after="Mage_Adminhtml">MageSf_OrderCustom_Adminhtml</MageSf_OrderCustom>
</modules>
</args>
</adminhtml>
</routers>
</admin>
Update your ordercustom.phtml You need to remove all other code from this file.
<form id="doc-form" name="doc-form" method="post" action="<?php echo Mage::helper('adminhtml')->getUrl('adminhtml/sfupload/upload',array('form_key' => Mage::getSingleton('core/session')->getFormKey())); ?>" enctype="multipart/form-data">
<label>Upload Document</label>
<input type="file" title="File" name="docname">
<button type="submit" title="Save"><span>Upload</span></button>
</form>
Best Answer
You need follow below steps
new hidden
input field to fromonclick="<?php echo $currentUrl = Mage::helper('core/url')->getCurrentUrl();?>"
for form submitfrom
<button id="<?php echo $seller->getMpassignproductId() ?>" class="button btn-cart customaddtocart" title="<?php echo $this->__('Add to Cart')?>" onclick="<?php echo $currentUrl = Mage::helper('core/url')->getCurrentUrl();?>">
Update:
Use this it should works: