1

I want to add captcha in test.phtml file and test.phtml file called from controller like below:

 public function execute()
{      

    $response = array();
    $post = $this->getRequest()->getParams();
      $this->_coreSession->start();
      $this->_coreSession->setProductsData($post);
      $resultJson = $this->resultJsonFactory->create();
    try {

      $block = $this->resultPageFactory->create()->getLayout()
            ->createBlock("Vendor\Modulename\Block\Popupdata")
             ->setTemplate("Vendor_Modulename::view/test.phtml")
            ->toHtml();

        /** @var array $response */
        $response = [
            'content' => $block,
        ];
    } catch (\Exception $exception) {
        $resultJson->setStatusHeader(
            \Zend\Http\Response::STATUS_CODE_400,
            \Zend\Http\AbstractMessage::VERSION_11,
            'Bad Request'
        );
        /** @var array $response */
        $response = [
            'message' => __('An error occurred')
        ];
        $this->_logger->critical($exception);
    }

    return $resultJson->setData($response);
} 
1

1 Answer 1

-1

add below code in phtml :

    <?php
                $blockPath = 'Magento\Captcha\Block\Captcha';
                $block = $this->getLayout()->createBlock($blockPath);
                $cap_data = array('form_id'=>'product-contact-cap','img_width'=>'50');
                $block->setData($cap_data);
                echo $block->toHtml();
            ?>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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