1

I add contact form in CMS page using this code -

   <form class="form contact"
  action="<?= $block->escapeUrl($block->getFormAction()) ?>"
  id="contact-form"
  method="post"
  data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
  data-mage-init='{"validation":{}}'>
<fieldset class="fieldset">
    <legend class="legend"><span><?= $block->escapeHtml(__('Write Us')) ?></span></legend><br />
    <div class="field note no-label"><?= $block->escapeHtml(__('Jot us a note and we’ll get back to you as quickly as possible.')) ?></div>
    <div class="field name required">
        <label class="label" for="name"><span><?= $block->escapeHtml(__('Name')) ?></span></label>
        <div class="control">
            <input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('name') ?: $this->helper('Magento\Contact\Helper\Data')->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
        </div>
    </div>
    <div class="field email required">
        <label class="label" for="email"><span><?= $block->escapeHtml(__('Email')) ?></span></label>
        <div class="control">
            <input name="email" id="email" title="<?= $block->escapeHtmlAttr(__('Email')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('email') ?: $this->helper('Magento\Contact\Helper\Data')->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
        </div>
    </div>
    <div class="field telephone">
        <label class="label" for="telephone"><span><?= $block->escapeHtml(__('Phone Number')) ?></span></label>
        <div class="control">
            <input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper('Magento\Contact\Helper\Data')->getPostValue('telephone')) ?>" class="input-text" type="text" />
        </div>
    </div>
    <div class="field comment required">
        <label class="label" for="comment"><span><?= $block->escapeHtml(__('What’s on your mind?')) ?></span></label>
        <div class="control">
            <textarea name="comment" id="comment" title="<?= $block->escapeHtmlAttr(__('What’s on your mind?')) ?>" class="input-text" cols="5" rows="3" data-validate="{required:true}"><?= $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')->getPostValue('comment')) ?></textarea>
        </div>
    </div>
    <?= $block->getChildHtml('form.additional.info') ?>
</fieldset>
<div class="actions-toolbar">
    <div class="primary">
        <input type="hidden" name="hideit" id="hideit" value="" />
        <button type="submit" title="<?= $block->escapeHtmlAttr(__('Submit')) ?>" class="action submit primary">
            <span><?= $block->escapeHtml(__('Submit')) ?></span>
        </button>
    </div>
</div>

How to change action url of contact page.It transfer localhost/sanjeev/contacts/index/ I show contact form in footer section without action redirection other page.

  • Did you added the above html code to your cms page or you have called phtml in your cms page? – Sukumar Gorai Nov 14 '18 at 7:40
  • I call phtml file using this code - {{block type="core/template" template="custom/yourform.phtml"}} – Gagan Nov 14 '18 at 9:35
  • In Magento 1 or 2 – PЯINCƏ Nov 14 '18 at 9:59
  • I used Magento 2 – Gagan Nov 14 '18 at 10:12
  • If you are using magento2 then it will not work. – Sukumar Gorai Nov 14 '18 at 10:14
0

In the phtml file of the contact form, you need to change the form action.

You need to set the action URL as the current URL of the page, from where the contact form is being submitted.

To do so, just open the form.phtml file in your theme and change:

action="<?= $block->escapeUrl($block->getFormAction()) ?>"

to:

action="<?= $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]) ?>"

I hope it would help.

|improve this answer|||||
  • After add this code in action , contact form is not submitted – Gagan Nov 14 '18 at 9:28
  • @Gagan, You have asked to change action URL, so the answer is correct. I think you want to change the URL where the user should redirect back after submitting the form. Please confirm? – Mohit Kumar Arora Nov 14 '18 at 9:49
  • yes, I want to no redirection done after submitting form. – Gagan Nov 14 '18 at 9:53
  • If you want to redirect back to the current page, I can help. Otherwise, you can submit the form using ajax. – Mohit Kumar Arora Nov 14 '18 at 9:54

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.