0

I'm very new to Magneto 2.3, I'm trying to get the primary key from URL, this is my code:

This is my import button, on click of this button, a function hit.

/app/code/EC/Customimport/view/adminhtml/templates/form/import.phtml

<div class="pp-buttons-container">
    <button class="import" id="<?php echo $block->getId() ?>" onclick="return false;">
        <span><span><span><?php echo 'Import'; ?></span></span></span>
    </button>
</div>
<script type="text/javascript">
    require(["jquery",'mage/url'], function($, url){
        "use strict";
        $(document).on('click','.col-action-grouped a',function(){
        });

        $(document).on('click','.import',function(){
            jQuery.ajax({
            url: '/admin/customimport/index/import',
            type: "POST",
            data: data: {url_value : url},
            success: function(response){
                console.log('ABCD');
            }
        });
        });

    });
</script>

This is the file and in that file their is a function execute, where I'm trying to get the primary key, please see the code below:

/app/code/EC/Customimport/Controller/Adminhtml/Index/Import.php

<?php

namespace EC\Customimport\Controller\Adminhtml\Index;

use EC\Customimport\Model\CustomimportFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;

class Import extends \Magento\Backend\App\Action
{
    protected $request;

    /**
     * @param Context $context
     */
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        Context $context,
        CustomimportFactory $modelCustomimportFactory
    ) {
        $this->request = $request;
        $this->modelCustomimportFactory = $modelCustomimportFactory;
        $this->resultFactory = $context->getResultFactory();
        parent::__construct($context);
    }

    public function execute(){
        ignore_user_abort(true);
        set_time_limit(0);

        //$id = $this->request->getParams('customimport_id'); 
        //print_r($id);
        //exit;

        $postData = $this->getRequest()->getPost(); 
        $id = $postData['customimport_id'];
        print_r($id);
        exit;
    }
}

enter image description here

  • are you want to get the id which show into URL, right? – Dhiren Vasoya May 15 at 10:57
  • $id = $this->request->getParam('customimport_id'); – Rutvee Sojitra May 15 at 10:58
  • $id = $this->getRequest->getParams(''customimport_id''); – aravind May 15 at 11:12
1

First you need to update your phtml ajax code like this.

$(document).on('click','.import',function(){
            jQuery.ajax({
            url: '/admin/customimport/index/import',
            type: "POST",
            data: {customimport_id : '<?php echo $this->getRequest()->getParam('icustomimport_id'); ?>'},
            success: function(response){
                console.log('ABCD');
            }
        });

Now replace your controller code like this.

$id = $this->getRequest()->getParams('customimport_id'); 
print_r($id);
exit;

Or try this code.

$postData = $this->getRequest()->getPost(); 
$id = $postData['customimport_id'];
print_r($id);
exit;
0

Try passing data like this in import.phtml

<div class="pp-buttons-container">
<button class="import" id="<?php echo $block->getId() ?>" onclick="return false;">
    <span><span><span><?php echo 'Import'; ?></span></span></span>
</button>
</div>
<script type="text/javascript">
require(["jquery",'mage/url'], function($, url){
    "use strict";
    $(document).on('click','.col-action-grouped a',function(){
    });

    $(document).on('click','.import',function(){
        jQuery.ajax({
        url: '/admin/customimport/index/import',
        type: "POST",
        data: {customimport_id :'Some Value'},
        success: function(response){
            console.log('ABCD');
        }
    });
    });

});

And get the value in controller like

use EC\Customimport\Model\CustomimportFactory;

use Magento\Framework\Controller\ResultFactory; use Magento\Backend\App\Action\Context;

class Import extends \Magento\Backend\App\Action { protected $request;

/**
 * @param Context $context
 */
public function __construct(
    \Magento\Framework\App\Request\Http $request,
    Context $context,
    CustomimportFactory $modelCustomimportFactory
) {
    $this->request = $request;
    $this->modelCustomimportFactory = $modelCustomimportFactory;
    $this->resultFactory = $context->getResultFactory();
    parent::__construct($context);
}

public function execute(){
    ignore_user_abort(true);
    set_time_limit(0);

    //$id = $this->request->getParams('customimport_id'); 
    //print_r($id);
    //exit;

    $postData = $this->getRequest()->getParams(); 
    $id = $postData['customimport_id'];
    print_r($id);
    exit;
}

}

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.