I already have a form and controller action, that send data without using ajax.How to do it with AJAX? I would appreciate your help!
view/frontend/templates/reviews.phtml
Controller/Index/Save.php
< ?php
namespace TrainingReviewsControllerIndex;
use MagentoFrameworkAppActionContext;
use TrainingReviewsModelReviewsFactory;
class Save extends MagentoFrameworkAppActionAction
{
protected $_test;
public function __construct(
Context $context,
ReviewsFactory $test
) {
$this->_test = $test;
parent::__construct($context);
}
public function execute()
{
$data = $this->getRequest()->getParams();
$test = $this->_test->create();
$test->setData($data);
if($test->save()){
$this->messageManager->addSuccessMessage(__('You saved
review'));
}else{
$this->messageManager->addErrorMessage(__('Review was not
saved.'));
}
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('reviews/index/index');
return $resultRedirect;
}
}
Controller/Index/Index.php
< ?php
namespace TrainingReviewsControllerIndex;
class Index extends MagentoFrameworkAppActionAction
{
public function execute()
{
$this->_view->loadLayout();
$this->_view->getLayout()->initMessages();
$this->_view->renderLayout();
}
}