Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery newsletter submit - loading message

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

like image 829
GuitarMaster Avatar asked Nov 11 '11 02:11

GuitarMaster


People also ask

How can I submit a form without refreshing the page?

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

How do I automatically submit a form without clicking?

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.


1 Answers

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>
like image 165
Gino Sullivan Avatar answered Oct 05 '22 17:10

Gino Sullivan