Parth Patel
Parth Patel

Reputation: 819

Reload current page without losing of user input

I create a Login page in which i want to reload my captcha code but i don't want to lose my information entered by the user.

File : Login.jsp

<body>
<b>REGISTER</b>
<p>Please fill up the form below.</p>
<s:form action="register" method="post">
    <s:textfield label="Enter username" key="userId" maxlength="5" size="30" id="userId"/>
    <s:password label="Enter password" key="userPsw" size="30" />
    <tr>
        <td></td>
        <td>
            <img src="<c:url value='simple-captcha.png' />" />
            <br />
            <p onclick="something()">Refresh</p>
        </td>
    </tr>
    <s:textfield label="Enter code" key="captchaResponse" size="30" />
    <s:submit value="Login" />
</s:form>

File : Javascript code

function something()
{
    window.location.reload(true);
}

window.onload = function()
{
    var a = sessionStorage.getItem('userId');
    if(a !== null) $('#inputName').val(name); 

    var b = sessionStorage.getItem('userPsw');
    if(b !== null) $('#userPsw').val(b);
}

window.onbeforeunload = function() {
    sessionStorage.setItem("userId", $('#userId').val());
    sessionStorage.setItem("userPsw", $('#userPsw').val());
}

On "Refresh" text i want to load my captcha code but i don't want to lose my data.

I refer this link : How to reload current page without losing any form data?

but still the query not resolved.

Upvotes: 0

Views: 6575

Answers (1)

Parth Patel
Parth Patel

Reputation: 819

After using different logic and syntax i found easy way to store user input if he/she press F5 or refreshing page.

File : Javascript code

<script>
    window.onload = function()
    {
        var a = sessionStorage.getItem('userId');
        if(a !== null){ 
            //alert(a);
            document.getElementById("userId").value = a; 
        }
    }

    window.onbeforeunload = function() {
        sessionStorage.setItem("userId", $('#userId').val());
    }
</script>

Upvotes: 2

Related Questions