My website is simple, it has text saying "Click here" , that upon clicking will open a modal-type contact form in the center of the screen.
Currently when this form is submitted, it delays 1 second, then calls on submit.php to send the data to my email.
Instead of redirecting to submit.php, I'd like to remain on the same page and simply close the pop-up contact form.
So it should go like this:
Click "Click here" > Fill in details in pop-up form > Click submit > Form submits after 1 second and then closes completely. (No re-direct)
You can view my demo site here.
Here is the form HTML:
<form method="post" action="submit.php" id="contactform" class="signin">
<h1>On submit, this window should close</h1>
<input name="name" id="name" type="text" class="feedback-input" placeholder="Name" />
<input name="email" id="email" type="text" class="feedback-input" placeholder="Email" required pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)" />
<div class="antispam">
<br /><input name="url" type="hidden" /></div>
<textarea name="message" id="message" class="feedback-input" placeholder="Write away!" required></textarea>
<button id="flybutton">
<p>Submit here</p>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve">
<path id="paper-plane-icon" d="M462,54.955L355.371,437.187l-135.92-128.842L353.388,167l-179.53,124.074L50,260.973L462,54.955z
M202.992,332.528v124.517l58.738-67.927L202.992,332.528z"></path>
</svg>
</button>
</form>
You can see that the form id is "contactform" and the button id is "flybutton"
The following scripts use this data to currently, delay 1 seccond, submit the form and then redirect to submit.php.
Instead, I need it to delay 1 second, submit the form, and then close the popup window (and black background).
Currently I have a script that will close the form via Escape key, so perhaps this could be implemented somehow? I feel that my scripts just need to be combined somehow.
Here are the scripts I have:
1 second delay + submit form
<script>
var $btn = $('#flybutton');
$("#contactform").validate({
submitHandler: function (form) {
fly(form);
}
});
$btn.on('fliyingEnd', function (e, form) {
form.submit();
})
function fly(form){
$btn.toggleClass('clicked');
$btn.find('p').text(function(i, text) {
return text === "Fire!" ? "Fire!" : "Fire!";
});
setTimeout(function () {
$btn.trigger('fliyingEnd', [form]);
}, 1000);
}
</script>
Close form box (#login-box) and black background (#mask) via escape key:
<script type="text/javascript">
$(document).keyup(function(e) {
if (e.keyCode == 27) {
$("#mask").fadeOut(300);
}
if (e.keyCode == 27) {
$("#login-box").fadeOut(300);
}
});
</script>
Another user helped with this script so I don't know it's purpose:
<script type="text/javascript">
$(document).ready(function() {
$('a.login-window').click(function() {
//Getting the variable's value from a link
var loginBox = $(this).attr('href');
//Fade in the Popup
$(loginBox).fadeIn(300);
//Set the center alignment padding + border see css style
var popMargTop = ($(loginBox).height() + 24) / 2;
var popMargLeft = ($(loginBox).width() + 24) / 2;
$(loginBox).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
// Add the mask to body
$('body').append('<div id="mask"></div>');
$('#mask').fadeIn(300);
return false;
});
// When clicking on the button close or the mask layer the popup closed
$('a.closest, #mask').bind('click', function() {
$('#mask , .login-popup').fadeOut(300 , function() {
$('#mask').remove();
});
return false;
});
});
</script>
Can you please tell me which code to edit in order to get the functionality I need?
I couldn't get jsFiddle working but here's my demo site again
UPDATE: I've incorporated AJAX, but am having an issue. Specifically, the form submits but it STILL redirects.
Included this underneath my form
<div id="result"></div>
and here is the script.. how do I make NO re-direct?
<script>
$(document).ready(function() {
/* Attach a submit handler to the form */
$("#contactform").submit(function(event) {
/* Stop form from submitting normally */
event.preventDefault();
/* Clear result div*/
$("#result").html('');
/* Get some values from elements on the page: */
var values = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "submit.php",
type: "post",
data: values,
success: function(){
alert("success");
$("#result").html('');
},
error:function(){
alert("failure");
$("#result").html('An error occurred');
}
});
});
});
</script>
Thanks a lot!