cancel
Showing results for 
Search instead for 
Did you mean: 

main.ERROR: Notice: Undefined variable: data in C:\xampp\htdocs\Magento\app\code\Vky\Test\Controller

main.ERROR: Notice: Undefined variable: data in C:\xampp\htdocs\Magento\app\code\Vky\Test\Controller

main.ERROR: Notice: Undefined variable: data in C:\xampp\htdocs\Magento\app\code\Vky\Test\Controller\Index\Save.php on line 25 [] []

I am getting error For Below Code.

 

<?php

namespace Vky\Test\Controller\Index;

use Magento\Framework\App\Action\Context;
use Vky\Test\Model\TestFactory;
use Magento\Framework\View\Result\PageFactory;
class Save extends \Magento\Framework\App\Action\Action
{
/**
* @var Test
*/
protected $_test;

public function __construct(
Context $context,
TestFactory $test
) {
$this->_test = $test;
parent::__construct($context);
}
public function execute()
{
$test = $this->_test->create();
$test->setData($data);
if($test->save()){
$this->messageManager->addSuccessMessage(__('You saved the data.'));
}else{
$this->messageManager->addErrorMessage(__('Data was not saved.'));
}
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('test/index/save');
return $resultRedirect;
}
}

1 REPLY 1

Re: main.ERROR: Notice: Undefined variable: data in C:\xampp\htdocs\Magento\app\code\Vky\Test\Contro

Hello @sekhar_n 

 

$test = $this->_test->create();
$test->setData($data);

 

in above code no data variable declared.

 

you need to declare that variable, let's example you want to get post value

then

$data = $this->getRequest->getParams()


if works then mark as a solution.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer