Hi all!
The “show message” option of the custom form block disappears too fast?
What about an alert box, fully customizable, that displays different texts and colors according to the failure or to the success of the form submit? Here we go!
Below or above your form block, add this code inside a custom code block.
Play with everything inside <style> </style>
to customize the alert box
Texts displayed can be changed within <div
Note that the alert box (whether it’s a failure or success) is set to last 6 seconds. You can change it at the end of the code, within the <script by changing the value 6000 (which is 6000 milliseconds - 6 seconds)
If you want the alert boxes to be on the top right of the screen, replace, in the <style,
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
by
top: 60px;
right: 80px;
/*remove transform: translate*/
Ok, full code here
In the footer of the page:
<div id="alertbox1" class="alertbox">
<div class="alertbox-header">Success</div>
<div class="alertbox-message">Your form has been submitted successfully.</div>
<button class="alertbox-close" onclick="document.getElementById('alertbox1').style.display='none';">×</button>
</div>
<div id="alertbox2" class="alertbox">
<div class="alertbox-header">Error</div>
<div class="alertbox-message">Sorry, there was an error submitting your form.</div>
<button class="alertbox-close" onclick="document.getElementById('alertbox2').style.display='none';">×</button>
</div>
In the header of the page:
<style>
.alertbox {
display: none;
position: fixed;
z-index: 1;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 350px;
border: 1px solid #ebebeb;
border-radius: 10px;
background-color: #FFFFFF;
box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.2);
padding: 15px;
text-align: center;
}
.alertbox-header {
font-size: 20px;
font-weight: 600;
margin-bottom: 10px;
color: green;
}
.alertbox-message {
font-size: 16px;
margin-bottom: 20px;
color: #182939;
}
.alertbox-close {
position: absolute;
top: 10px;
right: 10px;
font-size: 24px;
font-weight: bold;
border: none;
background-color: transparent;
cursor: pointer;
}
.alertbox.error {
background-color: #FFFFFF;
}
.alertbox.error .alertbox-header {
color: red;
}
</style>
<script>
window.addEventListener('block-loaded-form1', () => {
const alertbox1 = document.getElementById("alertbox1");
const alertbox2 = document.getElementById("alertbox2");
window.addEventListener("submit-form-success-form1", function () {
alertbox1.style.display = "block";
setTimeout(function() {
alertbox1.style.display = "none";
}, 6000);
});
window.addEventListener("submit-form-failure-form1", function () {
alertbox2.style.display = "block";
alertbox2.classList.add("error");
alertbox2.querySelector('.alertbox-header').classList.add("error");
setTimeout(function() {
alertbox2.style.display = "none";
alertbox2.classList.remove("error");
alertbox2.querySelector('.alertbox-header').classList.remove("error");
}, 6000);
});
});
</script>