1

I have an AJAX POST setup within my template file currently that has 3 values, which I want to POST to my Index.php within my Block.

However, my module is an AdminHTML module and this cannot be changed. From my understanding modules using AdminHTML use extra security measures and sending AJAX isn't quite as easy as doing it in raw jQuery -> PHP.

My AJAX Post request is below contained in view/adminhtml/templates/file.phtml:

    let post = $.post("Index.php",
         {
             "product_id": productID,
             "product_value": value['value'],
             "customer_group_id": custGroupID
         },
         function(data, status){
             console.log("status", status);
             console.log("Customer Group Price: ", data);
         });

In my PHP file (block/adminhtml/index/index.php) I have tried things such as:

$productID = $_POST['product_id'];
$custGroupID = $_POST['customer_group_id'];
$productVal = $_POST['product_value'];

And also:

if(isset($_POST['product_id']) && isset($_POST['customer_group_id']) && isset($_POST['product_value'])){
    echo(setProductCustomerGroupPrice($_POST['product_id'], $_POST['customer_group_id'], $_POST['product_value']));
}

However it always throws error messages. Likely because of this extra security layer.

0

when using ajax on adminhtml side you need to send data to controller. create index.php file inside your controller like (Controller/Adminhtml/Index.php) then do some like this

let post = $.post("your_routerName/Index.php",
     {
         "product_id": productID,
         "product_value": value['value'],
         "customer_group_id": custGroupID
     },
     function(data, status){
         console.log("status", status);
         console.log("Customer Group Price: ", data);
     });

Inside Index.php file

<?php

namespace yourcompany_name\module_name\Controller\Adminhtml;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index.php extends \Magento\Backend\App\Action
{

    protected $resultPageFactory;
    public $_storeManager;


    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,

        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,    

    ) {
         parent::__construct($context);
         $this->resultPageFactory = $resultPageFactory;    
         $this->resultJsonFactory = $resultJsonFactory;
         $this->_storeManager = $storeManager;

    }

    /**
     * Load the page defined in view/adminhtml/layout/exampleadminnewpage_helloworld_index.xml
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {


         $resultJson = $this->resultJsonFactory->create();
        $post = $this->getRequest()->getPostValue();



        return $resultJson->setData(['data' => 'your data']);

    }
  }
  • Within the ['data' => 'your data'] would I put something like ['product_id' => $post['product_id'] ? Also once I have my data within the controller how can I retrieve that data within my block file? – A. Fletcher Nov 20 '18 at 16:53
  • yes you can do like this. After that output result in your template jquery. Result will be like data.product_id. – Ashar Riaz Nov 20 '18 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.