Automatically submitting an asp.net form with javascript
<p>I need to automatically log users in to an asp.net application who will be logging in from a php webpage. I thought this would be fairly simple but am having problems getting it to work. I set up a remote test page that submits an emai and password to the login page. On the login page I added the following code - </p>

<pre><code>&lt;script runat="server"&gt;

void Page_Load(object sender, EventArgs e){

if (IsPostBack) {
    // It is a postback
} else {
    // It is not a postback

Session["email"]= Request.Form["email"];
Session["password"]= Request.Form["password"];
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "submitForm()", true);
}

&lt;/script&gt;


&lt;script type="text/javascript"&gt;
    function submitForm() {
    document.forms["form1"].elements["email"].value = "&lt;%= Session["email"] %&gt;";
    document.forms["form1"].password.value = "&lt;%= Session["password"] %&gt;";
    document.forms["form1"].submit();
    }
&lt;/script&gt;
</code></pre>

<p>If I remove the document.forms["form1"].submit(); line I am taken to the login page and the email and password are populated in the appropriate boxes, if I then click the submit button manually I get logged in. As a test I then removed only the document.forms["form1"].password.value = "&lt;%= Session["password"] %>"; line and it seemed to submit the form as I got a validation error saying that a password is required (as I would do submitting the login manually without a password)</p>

<p>However as it stands it takes me to the login page and only the email is populated, I assume both email and password have been sent and then email has dissapeared after summission but don't know why that would be the case (given that a manually submission works) am I missing something obvious?</p>

<p>Thanks</p>

<p>Form markup is - </p>

<pre><code>&lt;form id="form1" runat="server"&gt;


    &lt;table class="loginpanelouter" border="0" cellpadding="0" cellspacing="0" align="center"&gt;
        &lt;tr&gt;
            &lt;td class="loginlogo" colspan="2"&gt;
                &lt;asp:Image ID="CMSLogo" runat="server" /&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="logininvalid" colspan="2"&gt;
                &lt;asp:Label ID="ErrorMessage" runat="server"&gt;&lt;/asp:Label&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="loginforgot" colspan="2"&gt;
                &lt;asp:LinkButton ID="ForgottenPassBtn" CausesValidation="false" runat="server"&gt;Forgotten Password?&lt;/asp:LinkButton&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="logintext"&gt;
                Email:&amp;nbsp;&amp;nbsp;
            &lt;/td&gt;
            &lt;td valign="middle" width="200"&gt;
                &lt;asp:TextBox ID="email" CssClass="loginpagefields" runat="server" size="25"&gt;&lt;/asp:TextBox&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="logintext"&gt;
                &amp;nbsp;
            &lt;/td&gt;
            &lt;td width="240" valign="middle"&gt;
                &lt;asp:RegularExpressionValidator ID="emailValid" runat="server" ValidationExpression="[\w\.-]+(\+[\w-]*)?@([\w-]+\.)+[\w-]+"
                    ErrorMessage="Please enter a valid email address." ControlToValidate="email"
                    EnableClientScript="False"&gt;&lt;/asp:RegularExpressionValidator&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="logintext"&gt;
                Password:&amp;nbsp;&amp;nbsp;
            &lt;/td&gt;
            &lt;td width="200" valign="middle"&gt;
                &lt;asp:TextBox ID="password" CssClass="loginpagefields" runat="server" size="25" TextMode="password"&gt;&lt;/asp:TextBox&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td class="logintext"&gt;
                &amp;nbsp;
            &lt;/td&gt;
            &lt;td width="240" valign="middle"&gt;
                &lt;asp:RequiredFieldValidator ID="passwordRequired" runat="server" ErrorMessage="Please enter your Password"
                    Display="Static" ControlToValidate="password" EnableClientScript="False"&gt;&lt;/asp:RequiredFieldValidator&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td colspan="2"&gt;
                &lt;table&gt;
                    &lt;tr&gt;
                        &lt;td class="loginremeber"&gt;
                            &lt;asp:CheckBox ID="RememberLogin" runat="server" Text="Remember my sign in details"
                                ToolTip="Please select this box if you wish your sign in details to be remembered"&gt;
                            &lt;/asp:CheckBox&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                        &lt;/td&gt;
                        &lt;td width="120px" valign="middle" align="right"&gt;
                            &lt;asp:Button ID="LoginBtn" runat="server" CssClass="buttonstyle" Text="Sign in" ToolTip="Please click to sign in"&gt;
                            &lt;/asp:Button&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/table&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td colspan="2" height="35"&gt;
                &amp;nbsp;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
    &lt;/form&gt;
</code></pre>
