3

I am trying to get input data in the controller but controller not getting url params.

<?php

namespace Crud\Crudatfrontend\Controller\Result;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;

class Result extends \Magento\Framework\App\Action\Action
{

     /**
     * @var Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    protected $resultJsonFactory; 

    /**
     * @param Context     $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        JsonFactory $resultJsonFactory
        )
    {

        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory; 
        return parent::__construct($context);
    }


    public function execute()
    {
        var_dump($this->getRequest()->getParams());die("testing");
        //echo '<pre>';
        //print_r($this->getRequest('height')->getParams());exit;
        $height = $this->getRequest()->getParam('height');
        //$height = 5;
        $weight = $this->getRequest()->getParam('weight');
        $result = $this->resultJsonFactory->create();
        $resultPage = $this->resultPageFactory->create();

        $block = $resultPage->getLayout()
                ->createBlock('Crud\Crudatfrontend\Block\Index')
                ->setTemplate('Crud_Crudatfrontend::result.phtml')
                ->setData('height',$height)
                ->setData('weight',$weight)
                ->toHtml();

        $result->setData($block);
        return $result;
    } 
}

Here is my phtml file

<div class="row clearfix">
        <div class="col-md-12 column">

            <form name="form_height" method="POST" id="form_height">
                    <input type="text" name="height" class="form-control input-md">
                    <input type="text" name="weight" class="form-control input-md">
                    <input type="submit" id="calculateTotalSubmit"  name="calculate-total-submit" value="Calculate Total">
                </div> 
            </form>
        </div>
    </div>


<script>
require(['jquery'],function(){
    jQuery(document).ready(function() {
        alert("hi")
        jQuery("#form_height").submit(function(){

            var heightValue = jQuery("input[name='height']").val();
            var weightValue = jQuery("input[name='weight']").val();

            var url = "<?php echo $block->getBaseUrl().'crud/result/result/' ?>";
            jQuery.ajax({
            url: url,
            type: "POST",
            data: {height:heightValue,weight:weightValue},
            //data: jQuery(this).serialize(),
            success: function(response){
               // alert(heightValue);
                console.log(response.output);
                window.location.href = url;
            }
        });

        return false;
        });
    });
});
</script>

I am getting values in console.

  • you better use \Magento\Framework\App\Request\Http – fmsthird Apr 12 at 5:34
3

I have given demo extension link for you.
https://github.com/magento1993/ajaxphtml

  • Did you check this module? it's not working for me – Bhakti Thakkar Apr 12 at 6:15
  • Yes, this extension is created by me for learning purpose – Biren Patel Apr 12 at 6:17
  • which version have you tested on? – Bhakti Thakkar Apr 12 at 6:17
  • I am not able to redirect on cntrlphtml/index/post/ after clicking on calculate total button – Bhakti Thakkar Apr 12 at 6:19
  • it's ajax so it will not redirect if you want to redirect then you have to remove ajax – Biren Patel Apr 12 at 6:21
0

Try this using \Magento\Framework\App\Request\Http

<?php

namespace Crud\Crudatfrontend\Controller\Result;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;

class Result extends \Magento\Framework\App\Action\Action
{

    /**
     * @var Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var JsonFactory 
     */
    protected $resultJsonFactory;

    /** @var \Magento\Framework\App\Request\Http */
    protected $request;

    /**
     * Result constructor.
     * @param Context $context
     * @param PageFactory $resultPageFactory
     * @param JsonFactory $resultJsonFactory
     * @param \Magento\Framework\App\Request\Http $request
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        JsonFactory $resultJsonFactory,
        \Magento\Framework\App\Request\Http $request
    )
    {

        $this->request = $request;
        $this->resultPageFactory = $resultPageFactory;
        $this->resultJsonFactory = $resultJsonFactory;
        return parent::__construct($context);
    }

    /**
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $height = $this->request->getParam('height'); // or $height = $this->request->getPost('height'); 

        $weight = $this->request->getParam('height'); // or $weight = $this->request->getPost('height'); 

        $result = $this->resultJsonFactory->create();
        $resultPage = $this->resultPageFactory->create();

        $block = $resultPage->getLayout()
            ->createBlock('Crud\Crudatfrontend\Block\Index')
            ->setTemplate('Crud_Crudatfrontend::result.phtml')
            ->setData('height',$height)
            ->setData('weight',$weight)
            ->toHtml();

        $result->setData($block);
        return $result;
    }
}
  • 1
    I tried but it is still not working – Bhakti Thakkar Apr 12 at 6:15

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.