javascript - Refresh textfield after submitting -


i have code submits database without refresing, problem when sent, variables still remains in textfield

<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>   <script> $(document).ready(function(){     $("form").on('submit',function(event){     event.preventdefault();          data = $(this).serialize();          $.ajax({         type: "post",         url: "insert.asp",         data: data         }).done(function( msg ) {          });     }); }); </script> 

i thinking if there's way variables disappear textfiled after been sent database , bring alert fades automatically when data saved.

i think you'll want add desired behavior in callback following ajax request. you're using "done" callback, in understanding execute whenever ajax call completes, regardless of success or failure. if that's desire, add textfield clearing , alert code within callback, otherwise callback "success" may more appropriate.

i'm not entirely sure mean "alert"... if you're referring alert box, ie.: alert(), wouldn't suggest it. pause script until user clicks button, there no way kill alert once has been displayed. alert boxes aren't nicest thing present user imo. alternative define div somewhere in page, add success message jquery within success/done callback , set time out fades (included in example below). if you're looking more elaborate jquery ui has alternatives alert boxes in widgets.

maybe code can give ideas started:

<script> $(document).ready(function(){     $("form").on('submit',function(event){         event.preventdefault();          data = $(this).serialize();          $.ajax({         type: "post",         url: "insert.asp",         data: data         }).success(function() {              $("#feedback").append("<div class='messages' style='border:1px green solid; padding:2px; margin:5px;'>successfully submitted!</div>");              settimeout(function() {                  $(".messages").fadeout(function(){                     $(".messages").remove();                 });              }, 1000);              $("input[type=text]").val("");          });     }); }); </script> 

where there's div id="feedback" you'd display messages.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -