-1

I have created one JSP page to insert user information and to show inserted info:

To get my insert data page I have a link eg: mysite/insertInfo

First accept user information and validate them. If valid submit form.Else when validation errors has occured , redirect to same page and without losing any form datas. At servlet (or controller class) save data and redirect to same page. I used redirecting with jQuery as like below:

url = baseURL + "/saveUserInfo";
params = {};
params.name = $(".txtname').val();
params.email = $('.txtemail').val();
ajax(url, params, false, function(data) {
    $('.form').empty();
    if (data) {
        // SHOW SUCCESS INFO
        html = [];
        html.push("<div>Success</div>");
        $('. form').append(html.join(''));
    }
});

Second, I will show these were inserted before. Yes, it was simple and fine.

My problems is that when the user go to page mysite/insertInfo again, there has a form with inserted data. No, I don't want to see them again. I don't want to redirect to another page eg: success.jsp . I only want to use one JSP file. As description of Resetting a multi-stage form with jQuery, I tested with it.

Like $('#myform').trigger("reset"); before submitting this form. It is fine and reset. But when I reached successful page and go back insert form page via link, I see form with previous inserted data. How to handle it? Any suggestions would be appreciated. I think my browser may cached it but I am not sure.

Community
  • 1
  • 1
Cataclysm
  • 7,592
  • 21
  • 74
  • 123

2 Answers2

1

When you have saved the data successfully add this to clear each input field

$('form input').val("");
slash197
  • 9,028
  • 6
  • 41
  • 70
  • I assume that will similar with $('#myform').trigger("reset"); Did you mean my JSP should have boolean variable to check clear form data. Sorry for my forget to display , when validation was invalid , redirect to same page and without losing any form datas. – Cataclysm Jul 13 '13 at 10:49
0

Now , my problem was fined , By using sessionStorage from http://www.w3schools.com/html/html5_webstorage.asp . It is really fine by adding following scripts..

  <script type="text/javascript">
        function insert_previous_datas() {
            var name = sessionStorage.getItem("name");
            if (name) {
                $('#inputName').val(name);
            }
            var email = sessionStorage.getItem("email");
            if (email){
                $('#inputEmail').val(email);
            }
        }
        window.onload = function() {
            var isForReview = sessionStorage.getItem("forReview"); 
            if (isForReview) {
                insert_previous_datas();
                sessionStorage.removeItem("forReview");
            }
            var isForValidate  = sessionStorage.getItem("forValidate");
            if (isForValidate) {
                insert_previous_datas();
                var errorMsgName = sessionStorage.getItem("errorMsgName");
                if (errorMsgName) {
                    $('#errorMsgName').addClass("error");
                    $('#errorMsgName').text(errorMsgName);
                }
                var errorMsgEmail = sessionStorage.getItem("errorMsgEmail");
                if (errorMsgEmail) {
                    $('#errorMsgEmail').addClass("error");
                    $('#errorMsgEmail').text(errorMsgEmail);
                }
                sessionStorage.removeItem("forValidate");
            }
        }
    </script>
Cataclysm
  • 7,592
  • 21
  • 74
  • 123