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;
  • hello @Dhiren,did you found any solution for this issue?magento.stackexchange.com/questions/274520/… – jafar pinjar May 15 at 11:01
  • You mean only this code in execute() ... ??? @DhirenVasoya – Saif Zakir May 15 at 11:04
  • @SaifZakir yes, so this code give you 5 which show into your screen-sort. – Dhiren Vasoya May 15 at 11:05
  • @DhirenVasoya Got nothing please see updated code and image...... – Saif Zakir May 15 at 11:08
  • @SaifZakir I update the answer, and kindly check that you use $this->getRequest() into your code, note like $this->request. – Dhiren Vasoya May 15 at 11:14
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.