0

I'm looking to submit my form to the same page, but it just redirects to the admin dashboard.

Here's my .phtml file:

<?php 
    $action = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    if(isset($_POST['email_address'])){
        $email = $_POST['email_address'];
        echo $email;
    }
?>
<p>Please enteer the customers email address and press reset. You will be given the customers new password after.</p>
<form name="reset_password" method="POST" id="reset-password" autocomplete="off" action="<?php echo $block->getPostUrl(); ?>">
    <div class="admin__form-field-control">
        <input type="email" name="email_address" placeholder="Enter email here..." class="admin__control-text" action"<?php echo $action; ?>" />
    </div>
    <div class="admin__footer-main-actions">
        <input type="submit"  class="action-secondary" value="Reset" />
    </div>
</form>

How do I make the form submit to the same page without it redirecting? Thanks in advance.

1 Answer 1

0

You have not added your block class here so please use below code and update your class . At admin end we need a form key to submit any form .

<?php 

use \Magento\Backend\Block\Template\Context;
use \Magento\Framework\View\Element\Template;

class BlockClass extends Template
{
    public $formKey;


    public function __construct(
        Context $context,

    ) {

        $this->formKey = $context->getFormKey();
        parent::__construct($context);
    }
    public function getFormKey()
    {
        return $this->formKey->getFormKey();
    }
    public function getPostUrl()
   {
     //what ever your code that you have already
   }
}

and change your .phtml file to something like this

<?php 
    $action = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    if(isset($_POST['email_address'])){
        $email = $_POST['email_address'];
        echo $email;
    }
?>
<p>Please enteer the customers email address and press reset. You will be given the customers new password after.</p>
<form name="reset_password" method="POST" id="reset-password" autocomplete="off">

  <input name="form_key" type="hidden" value="<?php /* @noEscape */ echo $block->getFormKey()?>" />
    <div class="admin__form-field-control">
        <input type="email" name="email_address" placeholder="Enter email here..." class="admin__control-text" action"<?php echo $action; ?>" />
    </div>
    <div class="admin__footer-main-actions">
        <input type="submit"  class="action-secondary" value="Reset" />
    </div>
</form>

We have added a hidden field that with name "form_key" .

Hope this helps .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.