I have contact us buttons more than one in a page. when the user click on the contact us button i am showing the popup form. After user submitting the form i am showing thanku div.But when the user click on contact us button again form is coming. What i need is once the user submitted the form i need to show thanku div only until user refresh the page and also if he click on another contact us button in the same page i need to show thanku div.
How can i achive this.thanku and please correct me if anything wrong in my code
my phtml file
<div id="popup-modal">
<div class="contact-form-popup" style="display:none;">
<form class="form contact"
action="<?= $block->getUrl("popup/action/index") ?>"
id="contact-form"
method="post"
data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
data-mage-init='{"validation":{}}'>
<fieldset class="fieldset">
<legend class="legend"><span><?= $block->escapeHtml(__('Get in touch')) ?></span></legend>
<br/>
<div class="field note no-label"><?= $block->escapeHtml(__('Simply fill in your details and enquiry below and one of our team will be in touch as soon as possible.')) ?></div>
<div class="field firstname required">
<label class="label" for="name"><span><?= $block->escapeHtml(__('Your Name')) ?></span></label>
<div class="control">
<input name="name" id="name" title="<?= $block->escapeHtmlAttr(__('Name')) ?>"
class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field emailaddress required">
<label class="label" for="emailaddress"><span><?= $block->escapeHtml(__('Customer email address')) ?></span></label>
<div class="control">
<input name="emailaddress" id="emailaddress" title="<?= $block->escapeHtmlAttr(__('Email')) ?>"
class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field mobile">
<label class="label" for="mobile"><span><?= $block->escapeHtml(__('Phone number')) ?></span></label>
<div class="control">
<input name="mobile" id="mobile" title="<?= $block->escapeHtmlAttr(__('Phone number')) ?>"
class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field postcode">
<label class="label" for="postcode"><span><?= $block->escapeHtml(__('Customer postcode')) ?></span></label>
<div class="control">
<input name="postcode" id="postcode" title="<?= $block->escapeHtmlAttr(__('Customer postcode')) ?>"
class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field enquiry required">
<label class="label" for="enquiry"><span><?= $block->escapeHtml(__('Enquiry')) ?></span></label>
<div class="control">
<textarea name="enquiry" id="enquiry" title="<?= $block->escapeHtmlAttr(__('Enquiry')) ?>"
class="input-text" type="text" data-validate="{required:true}"></textarea>
</div>
</div>
<div class="field extension required">
<input type='hidden' name="extension" id="extension-name"
class="input-text" type="text" data-validate="{required:true}"/>
</div>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit" id='customer-contact' title="<?= $block->escapeHtmlAttr(__('Submit')) ?>"
class="action submit primary">
<span><?= $block->escapeHtml(__('Submit')) ?></span>
</button>
</div>
</div>
</form>
</div>
<div style="display:none" class="static-block-message">
<legend class="legend"><span><?= $block->escapeHtml(__('Get in touch')) ?></span></legend>
<br/>
<div class="field note no-label">
<?= $block->escapeHtml(__('Simply fill in your details and enquiry below and one of our team will be in touch as soon as possible.')) ?>
</div>
<?= $block->escapeHtml(__('Thanku you.A member of our team will be in touch shortly.'))?>
<div>
</div>
</div>
<script>
require(
[
'jquery',
'Magento_Ui/js/modal/modal'
],
function($, modal) {
jQuery(document).ready(function(){
var options = {
type: 'popup',
responsive: true,
innerScroll: true,
buttons: []
};
var openModal = modal(options, $('#popup-modal'));
jQuery('body').on('click', '.demo-banner .pagebuilder-button-primary', function(){
jQuery('.contact-form-popup').show();
jQuery('.static-block-message').hide();
jQuery('#popup-modal').modal('openModal');
});
jQuery('body').on('click', '#contact-form .action.submit', function(e){
e.preventDefault();
e.stopImmediatePropagation();
jQuery.ajax({
type: 'post',
url: '<?php echo $block->getUrl("popup/action/index") ?>',
data: jQuery('#contact-form').serialize(),
cache: false,
showLoader: 'true',
success: function(response) {
//alert('success');
jQuery('.contact-form-popup').hide();
jQuery('.static-block-message').show();
}
});
return false;
});
});
}
);
</script>
my controller file
<?php
namespace TestPopupControllerAction;
use ZendLogFilterTimestamp;
use MagentoStoreModelStoreManagerInterface;
use MagentoFrameworkControllerResultFactory;
class Index extends MagentoFrameworkAppActionAction
{
/**
* Recipient email config path
*/
const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email';
/**
* @var MagentoFrameworkMailTemplateTransportBuilder
*/
protected $_transportBuilder;
/**
* @var MagentoFrameworkTranslateInlineStateInterface
*/
protected $inlineTranslation;
/**
* @var MagentoFrameworkAppConfigScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
* @var MagentoFrameworkEscaper
*/
protected $_escaper;
/**
* @param MagentoFrameworkAppActionContext $context
* @param MagentoFrameworkMailTemplateTransportBuilder $transportBuilder
* @param MagentoFrameworkTranslateInlineStateInterface $inlineTranslation
* @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
* @param MagentoStoreModelStoreManagerInterface $storeManager
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkMailTemplateTransportBuilder $transportBuilder,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkEscaper $escaper
) {
parent::__construct($context);
$this->_transportBuilder = $transportBuilder;
$this->inlineTranslation = $inlineTranslation;
$this->scopeConfig = $scopeConfig;
$this->storeManager = $storeManager;
$this->_escaper = $escaper;
}
/**
* Post user question
*
* @return void
* @throws Exception
*/
public function execute()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$post = $this->getRequest()->getPostValue();
//print_r($post);
//die("hello");
if (!$post) {
$this->_redirect('/');
return;
}
$this->inlineTranslation->suspend();
try {
$postObject = new MagentoFrameworkDataObject();
$postObject->setData($post);
$error = false;
$sender = [
'name' => $this->_escaper->escapeHtml($post['name']),
'email' => $this->_escaper->escapeHtml($post['emailaddress']),
];
$storeScope = MagentoStoreModelScopeInterface::SCOPE_STORE;
$transport = $this->_transportBuilder
->setTemplateIdentifier('popup_email') // this code we have mentioned in the email_templates.xml
->setTemplateOptions(
[
'area' => 'frontend',
'store' => $this->storeManager->getStore()->getId()
]
)
->setTemplateVars([
'name' => $post['name'],
'email' => $post['emailaddress'],
'mobile' => $post['mobile'],
'postcode' => $post['postcode'],
'enquiry' => $post['enquiry'],
])
->setFrom($sender)
->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
->addTo('mani@gmail.com')
-
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
//$this->messageManager->addSuccess(
//__('Thanks for contacting us with your comments and questions. We'll respond to you very soon.')
//);
//$resultRedirect->setUrl($this->_redirect->getRefererUrl());
//$this->_redirect('/');
return;
} catch (Exception $e) {
$this->inlineTranslation->resume();
$this->messageManager->addError(__('We can't process your request right now. Sorry, that's all we know.'.$e->getMessage())
);
//$resultRedirect->setUrl($this->_redirect->getRefererUrl());
//$this->_redirect('/');
return;
}
}
}