Magento 2 Forms – How to Submit Form Action to a Controller

controllersformsmagento2

I have created a cms page and try to add form in cms page using phtml. and i need a form action to a controller. but shows an error
app/code/Ventor/Pricebeat/view/frontend/templates/pricebeat.phtml

<form class="form" action="User/Post" method="post">

<fieldset class="fieldset">
    <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Test Form') ?></span></legend><br>
    <div class="field required">
        <label for="number" class="label">
            <span><?php /* @escapeNotVerified */ echo __('Input') ?></span>
        </label>

        <div class="control">
            <input type="text" id="number"
                   name="number"
                   class="input-text">
        </div>
    </div>
</fieldset>

<div class="actions-toolbar">
    <div class="primary">
        <button type="submit" class="action submit primary" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>">
            <span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
        </button>
    </div>
</div>

  </form>

app/code/Ventor/Pricebeat/Controller/User/Post.php

<?php

 namespace Ventor\Pricebeat\Controller\User;


 use Magento\Framework\App\Action\Action;
 use Magento\Framework\App\Action\Context;
 use Magento\Framework\View\Result\PageFactory;

 class Post extends Action
  {
protected $resultPageFactory;

public function __construct(Context $context, PageFactory $pageFactory)
{
    $this->resultPageFactory = $pageFactory;
    parent::__construct($context);
}

public function execute()
{
   echo "hello from the controller";
   exit();
    $resultPage = $this->resultPageFactory->create();

    return $resultPage;
}
}

app/code/Ventor/Pricebeat/etc/frontend/routes.xml

<?xml version="1.0"?>

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

Best Answer

Replace your form with below code:

<form class="form" action="<?= $block->getUrl('pricebeat/user/post'); ?>" method="post">
    <fieldset class="fieldset">
        <legend class="legend"><span><?php /* @escapeNotVerified */ echo __('Test Form') ?></span></legend><br>
        <div class="field required">
            <label for="number" class="label"><span><?php /* @escapeNotVerified */ echo __('Input') ?></span></label>
            <div class="control">
                <input type="text" id="number" name="number" class="input-text"/>
            </div>
        </div>
    </fieldset>
    <div class="actions-toolbar">
        <div class="primary">
            <button type="submit" class="action submit primary" title="<?php /* @escapeNotVerified */ echo __('Submit') ?>">
                <span><?php /* @escapeNotVerified */ echo __('Submit') ?></span>
            </button>
        </div>
    </div>
</form>

You need to use action url like below:

<?= $block->getUrl('pricebeat/user/post'); ?>

OR

<?= $this->getUrl('pricebeat/user/post'); ?>
Related Topic