Reputation: 663
I've seen a few similar questions but couldn't find anything relevant..
I have a basic form capturing user email all I want is that when form submits it does not load a blank page, instead just re-directs back to the empty form again.
<?php
if ($thanks === false) {
echo form::open('', array('id'=>'footer_email'));
echo '<div id="footer_box">';
echo '<div class="footer_box_left">';
echo form::input('form[email]', $val, array('class'=>'required', 'id'=>'email', 'placeholder'=>'Enter email address'));
echo '</div>';
echo form::submit('btnSubmit', 'Sign Up', array('id'=>'btnSubmit', 'class'=>'footer_box_submit'));
echo '</div>';
echo form::close();
}
?>
$(document).ready(function () {
var validator = $("#footer_email").validate({
errorLabelContainer: $("#footer_email div.error"),
meta: "validate",
invalidHandler: function (form, validator) {
alert("Please enter your email address first.");
},
submitHandler: function (form) { // on submit
if ($(form).valid())
form.submit();
alert("Thank you for sharing your email address. \nKarina");
return false;
}
});
});
Can anybody show me where I'm going wrong/would I be better to use ajax to achieve this?
Upvotes: 0
Views: 1321
Reputation: 2957
You can do this:
<?php
if ($thanks === false) {
echo form::open('', array('id'=>'footer_email'));
echo '<div id="footer_box">';
echo '<div class="footer_box_left">';
echo form::input('form[email]', $val, array('class'=>'required', 'id'=>'email', 'placeholder'=>'Enter email address'));
echo '</div>';
echo form::submit('btnSubmit', 'Sign Up', array('id'=>'btnSubmit', 'class'=>'footer_box_submit'));
echo '</div>';
echo form::close();
} else {
echo '<script>alert("Thank you for sharing your email address. \nKarina");</script>';
echo form::open('', array('id'=>'footer_email'));
echo '<div id="footer_box">';
echo '<div class="footer_box_left">';
echo form::input('form[email]', $val, array('class'=>'required', 'id'=>'email', 'placeholder'=>'Enter email address'));
echo '</div>';
echo form::submit('btnSubmit', 'Sign Up', array('id'=>'btnSubmit', 'class'=>'footer_box_submit'));
echo '</div>';
echo form::close();
}
?>
Upvotes: 1
Reputation: 643
Use Ajax to keep it fast and no many refresh
$(document).ready(function () {
var validator = $("#footer_email").validate({
errorLabelContainer: $("#footer_email div.error"),
meta: "validate",
invalidHandler: function (form, validator) {
alert("Please enter your email address first.");
},
submitHandler: function (form) { // on submit
if ($(form).valid()) {
$.ajax({
type: "POST",
url: url,
data: $(form).serialize()
}).done(function (data) {
alert("Thank you for sharing your email address. \nKarina");
$(form).reset();
}).fail(function () {
alert("Sorry Try again. \nKarina");
});
}
return false;
}
});
});
Upvotes: 0
Reputation: 3268
Since you are using
form.submit();
It makes the usual form submit like you were hitting the 'Send' button. What you want to do here is send the form serialized through Ajax,
var formData = $(form).serialize();
$.post('[HERE YOUR SUBMIT TARGET URL]', formData, function (success){
///Redirect to empty form, or clear actual form
});
Upvotes: 0