0

So im building a simple widget - that sends contact data though ajax to controller. But it is not working im getting a Uncaught Error: Call to a member function create() on null in

Layout - Vendor/Module/view/frontend/layout/ContactFormular_index_post.xml

<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
    <title>Kontakt Formular</title>
</head> 
<body>
    <referenceContainer name="content">
        <block class="Ereactor\ContactFormular\Block\Widget\KontaktFormular" before="-" template="Ereactor_ContactFormular::KontaktFormular.phtml" />
    </referenceContainer>
</body>

PHTML -Vendor/Module/view/frontend/templates/widget/KontaktFormular.phtml

<form id="contactForm" method="post">
        <input type="hidden" name="receiver_email_address" value="<?=$block->getData('Email') ?>">
        <div class="card bg-light border-0 rounded-0 mb-5 p-3">
            <div class="card-body mb-3">
                <div id="messages_product_view"></div>
                <p class="card-text font-weight-bold">Book et møte med oss</p>
                <div class="row">
                    <div class="col-11 col-lg-6">
                        <div class="field mb-3">
                            <div class="input-box">
                                <label class="mb-1 ml-2" for="name">Navn</label>
                                <input placeholder="Fornavn Etternavn" name="name" id="name" title="Navn" value="" class="input-text form-control required-entry p-3" type="text">
                            </div>
                        </div>
                        <div class="field mb-3">
                            <div class="input-box">
                                <label class="mb-1 ml-2" for="email">E-post</label>
                                <input placeholder="E-post" name="email" id="email" title="Epost" value="" class="input-text form-control required-entry validate-email p-3" type="text">
                            </div>
                        </div>
                        <div class="field mb-3">
                            <div class="input-box">
                                <label class="mb-1 ml-2" for="telephone">Telefonnummer</label>
                                <input placeholder="Telefonnummer" name="telephone" id="telephone" title="Telefon" class="input-text form-control required-entry p-3" type="text">
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-lg-6">
                        <div class="input-box">
                            <label class="mb-1 ml-2" for="comment">Melding</label>
                            <textarea placeholder="Skriv her" name="comment" id="comment" title="Kommentar" class="required-entry input-text form-control p-3" cols="5" rows="10"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="card-footer bg-light border-0 p-0 position-absolute text-center">
                <button type="submit" class="btn btn-warning mb-n3">Send</button>
            </div>
        </div></form><script>require(['jquery'],function(){
jQuery(document).ready(function() {
    jQuery("#contactForm").submit(function(){

        var name = jQuery("input[name='name']").val();
        var email = jQuery("input[name='email']").val();
        var telephone = jQuery("input[name='telephone']").val();
        var comment = jQuery("input[name='comment']").val();

        jQuery.ajax({
        url : 'ContactFormular/index/post',
        type: "POST",
        data: {name:name, email:email, telephone:telephone, comment:comment},
        showLoader: true,
        cache: false,
        success: function(response){
            console.log(response.output);
        }
        
    });
    return false;
    });
}); });

Block - Vendor/Module/Block/Widget/KontaktFormular.php

    <?php
namespace Ereactor\ContactFormular\Block\Widget;

use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface;

class KontaktFormular extends Template implements BlockInterface
{
protected $_template = "widget/KontaktFormular.phtml";


public function getName()
{
    return $this->getName();
}

public function getEmail()
{
    return $this->getEmail();
}

public function getTelephone()
{
    return $this->getTelephone();
}

public function getComment()
{
    return $this->getComment();
}

}

Controller - Vendor/Module/Controller/Index/Post.php

    <?php

namespace Ereactor\ContactFormular\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\Action\Context;

class Post extends Action {

     protected $resultJsonFactory;

    public function __construct(
    Context $context,
    JsonFactory $resultJsonFactory) 
    {           
       $this->resultJsonFactory = $resultJsonFactory; 
       return parent::__construct($context);
    }


    public function execute() {
    /** @var \Magento\Framework\Controller\Result\Json $result */
    $result = $this->resultJsonFactory->create(ResultFactory::TYPE_JSON);
    return $result->setData(['success' => true]);
   } 
}

The workflow - user comes on KontaktFormular.phtml page > enters input > call submit > sends the data. Just for now.

  • While trying to call controller action using AJAX, it is not working im getting this error

[20-Aug-2020 08:47:53 UTC] PHP Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Framework\Controller\Result\JsonFactory::create() must be of the type array, string given, called in /home/clean/public_html/magento2/submodules/ContactFormular/Controller/Index/Post.php on line 25 and defined in /home/clean/public_html/magento2/vendor/magento/framework/Controller/Result/JsonFactory.php:48 Stack trace: #0 /home/clean/public_html/magento2/submodules/ContactFormular/Controller/Index/Post.php(25): Magento\Framework\Controller\Result\JsonFactory->create('json') #1 /home/clean/public_html/magento2/vendor/magento/framework/App/Action/Action.php(108): Ereactor\ContactFormular\Controller\Index\Post->execute() #2 /home/clean/public_html/magento2/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http)) #3 /home/clean/public_html/magento2/vendor/magento/framework/Interception/Interceptor.php(138): Ereactor\ContactFormular\Controller\Index\Post\Interceptor->___callP in /home/clean/public_html/magento2/vendor/magento/framework/Controller/Result/JsonFactory.php on line 48

0

Please run

php bin/magento setup:upgrade

It will be fixed.

| improve this answer | |

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.