Ved
Ved

Reputation: 8767

Submit a form when page loads - struts 2

I want to submit a s:form when page loads. here is my code :

<s:form id="login_form" action="" method="post">
<s:textfield label="User name" id="login_form_username"
            name="username" value="abc.xyz" cssClass="txtbox"
            required="true" />
        <s:password label="Password" id="login_form_password" name="password"
            value="pass" cssClass="txtbox" required="true" />
        <s:submit value="Login" />
    </s:form>

Actually as shown in above code, I will set uname and pwd dynamically and then want to submit this form directly using javascript.

I tried this :

<script type="text/javascript"> 
      document.forms.login_form.submit();
</script>

but had no success.

Please help... Thanks..

Upvotes: 0

Views: 11903

Answers (4)

Butani Vijay
Butani Vijay

Reputation: 4239

you can use Jquery code:

document.ready(function(){
  $("#login_form").submit();
});

dont forget to specify action attribute of form.

Note: to use jquery you need to add jquery library.

Upvotes: 1

Raki
Raki

Reputation: 361

If you want to submit through java script means you have to give code like this eg:

If your from name is *login_form* in java script then you can give like this

function submitform()
{
    document.forms["login_form"].submit();
}


<s:form id="login_form" name="login_form" action="your action name" method="post">
<s:textfield label="User name" id="login_form_username"
            name="username" value="abc.xyz" cssClass="txtbox"
            required="true" />
        <s:password label="Password" id="login_form_password" name="password"
            value="pass" cssClass="txtbox" required="true" />
        <s:submit value="Login" onClick="submitform()"/>
</s:form>

Here you should remember 2 important points:

1)without action name it will not work because, for going to the struts.xml you need action name right in your code i didn't see the action name.

2)Secondly, in your code you written some java script code but, you not called that function name either through submit button nor through body onload function. I will assume like for posting purpose you did this coding k that is not the problem.

what i was showed above code that is one way. Another way is without submit button your code should run means, you can type like this in a jsp page

<META HTTP-EQUIV="Refresh" CONTENT="1;URL=youractionname.action">

you have to write this code before body only then your jsp page works like what you want.

Here i used for both the codes action name(youractionname) means without action name in struts2 page won't go to the action class

Bottom Line:Action name is important in struts2

Upvotes: 0

coding_idiot
coding_idiot

Reputation: 13734

<s:form id="login_form" action="" method="post">

As you can see the action to which the form will be submitted is missing. How will the above javascript know on which action to submit this form to. Try filling in a action name.

Upvotes: 0

Arun
Arun

Reputation: 1177

document.forms.login_form.submit() throwing undefined error.instead of that you can use document.forms[0].submit()

you can use onload attribute for page loads works

<script type="text/javascript"> 
    function submit(){
      document.forms[0].submit();
    }
</script>

  <body onload="submit()">
  <s:form id="login_form" action="" method="post">
       <s:textfield label="User name" id="login_form_username"
            name="username" value="abc.xyz" cssClass="txtbox"
            required="true" />
        <s:password label="Password" id="login_form_password" name="password"
            value="pass" cssClass="txtbox" required="true" />
        <s:submit value="Login" />
    </s:form>

 </body>

Upvotes: 0

Related Questions