i have a newsletter form i use on my site using ajax with jquery. i want to show to a user a wait message. what is the best option?
heres what i have so far:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#submit').click(function(e) {
    $.ajax({
      type: "POST",
      url: '/save.php',
      data:  $('#form').serialize(),
      cache: false,
      success: function(result) {
        // my code when success
      }
    });
  });
});
</script>
<div id="newsletter">
  <form id="form">
  <label for="email">Your Email*:</label>
  <input name="email" value="" type="text" id="email" size="30" maxlength="255" />
  <span id="submit">Submit</span>
  </form>
</div>
thanks
Use jQuery's submit event to handle the form submit, add return false; at the end of the submit handle function to prevent the page to reload. return false ; }); });
Submit a Form Using JavaScript The most simple way to submit a form without the submit button is to trigger the submit event of a form using JavaScript. In the below example we are going to create a function to submit a form. We will set that function at onclick event of a div tag.
Here's what you can do:
Create a div (message dialog) and show when the user press on submit and hides it when the ajax is completed. I would also recommend to use the jQuery Validation plugin to validate the email.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#submit').click(function(e) {
    // validation
    $.ajax({
      type: "POST",
      url: '/file.php',
      data:  $('#form').serialize(),
      cache: false,
      success: function(result) {
        // do what ever you need
      },
      error: function (response, desc, exception) {
        // alert some message
      },
      beforeSend: function() {
        $('#loader').fadeIn(1000);
      },
      complete: function() {
        $('#loader').fadeOut(1000);
      },
    });
  });
});
</script>
<style type="text/css">
  #loader { display: none; /* and other css youy need like border, position, etc... */ }
</style>
<div id="loader">loading ...</div>
<div id="newsletter">
  <form id="form">
  <label for="email">Your Email*:</label>
  <input name="email" value="" type="text" id="email" size="30" maxlength="255" />
  <span id="submit">Submit</span>
  </form>
</div>
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With