2

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>

2

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'); ?>
1
<form class="form" action="<?php echo $this->getUrl('pricebeat/user/post'); ?>" method="post">

error also in namespace module

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

<?php

 namespace Ventor\Pricebeat\Controller\User;
 ...
0

Change action of form user/post to

/pricebeat/user/post

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.