0

I am trying to save form details using AJAX. Below is my Code.

etc/adminhtml/routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="adminpath" frontName="adminpath">
            <module name="My_Module"/>
        </route>
    </router>
</config>

view/adminhtml/templates/page.phtml

<form class="authorize-form">

            <div class="form-group">
                <label class="control-label col-sm-2" for="email">name:</label>
                <div class="col-sm-10">
                  <input type="text" name = "name" class="form-control" id="name" placeholder="Enter name">
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-2" for="pwd">Email:</label>
                <div class="col-sm-10"> 
                  <input type="text" name = "email" class="form-control" id="email" placeholder="Enter email">
                </div>
            </div>

            <button type="button" id="submit" onclick="formsave()" class="btn btn-primary">Save</button>    

        </form>
<script>

 function formsave()
    {
        var name = $('#name').val();
        var email = $('#email').val();
        $.ajax({
            url: '<?php echo $block->getUrl('adminpath/ajaxcontroller'); ?>',
            showLoader: true,
            data: {name:name,email:email},
            type: "POST",
            success: function(result){
                alert("I am inside the success");
                console.log(result);
            }
        });
    }
</script>

controller/adminhtml/Ajaxcontroller

<?php

namespace My\Module\Controller\Adminhtml;

class Ajaxcontroller extends \Magento\Backend\App\Action
{
    protected $resultPageFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        if($this->getRequest()->isAjax()){
            echo "Testing";
        }
        return false;
    }
}

Can anyone help me what's wrong here. It's not working for me.

1

You controller should be at path:

My\Module\Controller\Adminhtml\Ajaxcontroller\Index.php

You are using the action url as <?php echo $block->getUrl('adminpath/ajaxcontroller'); ?> which will call adminpath route's, ajaxcontroller's index action.

<?php echo $block->getUrl('adminpath/ajaxcontroller'); ?>

and

<?php echo $block->getUrl('adminpath/ajaxcontroller/index'); ?>

both are same in short.

Add Index.php at path My\Module\Controller\Adminhtml\Ajaxcontroller with below code:

namespace My\Module\Controller\Adminhtml\Ajaxcontroller;

class Index extends \Magento\Backend\App\Action
{
    protected $resultPageFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        if($this->getRequest()->isAjax()){
            echo "Testing";
        }
        return false;
    }
}
  • Thank you @himmatpaliwal . It is working now – Sammu Sundar Dec 3 '18 at 10:43
  • Great! Happy to help :) – Himmat Paliwal Dec 3 '18 at 10:44
  • url: '<?php echo $block->getUrl('adminpath/ajaxcontroller', ['form_key' => $block->getFormKey()]); ?>' // This is working url: '<?php echo $block->getUrl('adminpath/ajaxcontroller') ?>' // This is not working. please explain why this thing happnes – Sammu Sundar Dec 3 '18 at 10:50
  • No, it would also not work. Can you check by renaming My\Module\Controller\Adminhtml\Ajaxcontroller\Index.php to any other name? – Himmat Paliwal Dec 3 '18 at 11:03

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.