简体   繁体   中英

Prevent form “on submit” loading blank page

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?

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();

} 
?>

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
});

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;
          }
       });

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM