开发者

javascript form post submit working in IE but not Chrome

Dynamicaly adding a form to a page and submitting it in c#. Works fine in IE but just realized its not working in chrome. Im doing it as a function of a PayPal Transaction Object I built for a little ecomerce app. Here is the code.

    private void PayPalPostScriptLONG(Page page)
    {
        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script language='javascript'>");
        if (UserID == string.Empty || EventID == string.Empty || TransactionType == string.Empty)
        {
            strScript.Append("alert('missing event, user, or transactiontype');");
        }
        else
        {
            strScript.AppendFormat("myform=document.createElement('form'); myform.method='post'; myform.target='_top'; myform.action='{0}'; myform.name='frmPP';", Paypal);
            strScript.AppendFormat("input1=document.createElement('input'); input1.type='hidden'; input1.name='cmd'; input1.value='{0}'; myform.appendChild(input1);", Cmd);
            strScript.AppendFormat("input2=document.createElement('input'); input2.type='hidden'; input2.name='business'; input2.value='{0}'; myform.appendChild(input2);", Business);
            strScript.AppendFormat("input3=document.createElement('input'); input3.type='hidden'; input3.name='item_name'; input3.value='{0}'; myform.appendChild(input3);", Item_name);
            strScript.AppendFormat("input4=document.createElement('input'); input4.type='hidden'; input4.name='amount'; input4.value='{0}'; myform.appendChild(input4);", Amount);
            strScript.AppendFormat("input5=document.createElement('input'); input5.type='hidden'; input5.name='return'; input5.value='{0}'; myform.appendChild(input5);", ReturnURL);
            strScript.AppendFormat("input6=document.createElement('input'); input6.type='hidden'; input6.name='custom'; input6.value='{0}'; myform.appendChild(input6);", Custom);
            strScript.AppendFormat("input7=document.createElement('input'); input7.type='hidden'; input7.name='on0'; input7.value='uID'; myform.appendChild(input7);");
            strScript.AppendFormat("input8=document.createElement('input'); input8.type='hidden'; input8.name='os0'; input8.value='{0}'; myform.appendChild(input8);", UserID);
            strScript.AppendFormat("input9=document.createElement('input'); input9.type='hidden'; input9.name='on1'; input9.value='eID'; myform.appendChild(input9);");
            strScript.AppendFormat("inpu开发者_运维知识库t10=document.createElement('input'); input10.type='hidden'; input10.name='os1'; input10.value='{0}'; myform.appendChild(input10);", EventID);
            strScript.AppendFormat("input11=document.createElement('input'); input11.type='hidden'; input11.name='on2'; input11.value='type'; myform.appendChild(input11);");
            strScript.AppendFormat("input12=document.createElement('input'); input12.type='hidden'; input12.name='os2'; input12.value='{0}'; myform.appendChild(input12);", TransactionType);
            strScript.AppendFormat("input13=document.createElement('input'); input13.type='hidden'; input13.name='address1'; input13.value='{0}'; myform.appendChild(input13);", Address1);
            strScript.AppendFormat("input14=document.createElement('input'); input14.type='hidden'; input14.name='address2'; input14.value='{0}'; myform.appendChild(input14);", Address2);
            strScript.AppendFormat("input15=document.createElement('input'); input15.type='hidden'; input15.name='city'; input15.value='{0}'; myform.appendChild(input15);", City);
            strScript.AppendFormat("input16=document.createElement('input'); input16.type='hidden'; input16.name='email'; input16.value='{0}'; myform.appendChild(input16);", Email);
            strScript.AppendFormat("input17=document.createElement('input'); input17.type='hidden'; input17.name='first_name'; input17.value='{0}'; myform.appendChild(input17);", First_name);
            strScript.AppendFormat("input18=document.createElement('input'); input18.type='hidden'; input18.name='last_name'; input18.value='{0}'; myform.appendChild(input18);", Last_name);
            strScript.AppendFormat("input19=document.createElement('input'); input19.type='hidden'; input19.name='state'; input19.value='{0}'; myform.appendChild(input19);", State);
            strScript.AppendFormat("input20=document.createElement('input'); input20.type='hidden'; input20.name='zip'; input20.value='{0}'; myform.appendChild(input20);", Zip);
            strScript.AppendFormat("input21=document.createElement('input'); input21.type='hidden'; input21.name='night_ phone_a'; input21.value='{0}'; myform.appendChild(input21);", PhonePart1);
            strScript.AppendFormat("input22=document.createElement('input'); input22.type='hidden'; input22.name='night_ phone_b'; input22.value='{0}'; myform.appendChild(input22);", PhonePart2);
            strScript.AppendFormat("input23=document.createElement('input'); input23.type='hidden'; input23.name='night_ phone_c'; input23.value='{0}'; myform.appendChild(input23);", PhonePart3);


            strScript.Append("document.appendChild(myform);");
            strScript.Append("myform.submit();");
        }
        strScript.Append("</script>");
        page.ClientScript.RegisterClientScriptBlock(typeof(Page), "PPSubmit", strScript.ToString());
    }

Any help would be greatly appreciated!!

Thanks!


i ended up taking jumpdarts advice and using the following code:

    private string GetForm()
    {            
        StringBuilder ppForm = new StringBuilder();
        ppForm.AppendFormat("<Form name=\"frmPP\" id=\"frmPP\" action=\"{0}\" method=\"post\" target=\"_top\" \">", Paypal);
        AddInput(ppForm,"cmd",Cmd);
        AddInput(ppForm,"business",Business);
        AddInput(ppForm,"item_name",Item_name);
        AddInput(ppForm,"amount",Amount);
        AddInput(ppForm,"return",ReturnURL);
        AddInput(ppForm,"custom",Custom);
        AddInput(ppForm,"on0",UserID);
        AddInput(ppForm,"os0","uID");
        AddInput(ppForm,"on1",EventID);
        AddInput(ppForm,"os1","eID");
        AddInput(ppForm,"on2",TransactionType);
        AddInput(ppForm,"os2","type");
        AddInput(ppForm,"address1",Address1);
        AddInput(ppForm,"address2",Address2);
        AddInput(ppForm,"city",City);
        AddInput(ppForm,"email",Email);
        AddInput(ppForm,"first_name",First_name);
        AddInput(ppForm,"last_name",Last_name);
        AddInput(ppForm,"state",State);
        AddInput(ppForm,"zip",Zip);
        AddInput(ppForm,"night_ phone_a",PhonePart1);
        AddInput(ppForm,"night_ phone_b",PhonePart2);
        AddInput(ppForm,"night_ phone_c",PhonePart3);

        return ppForm.ToString();
    }

    private void AddInput(StringBuilder ppForm, string name, string value)
    {
        ppForm.AppendFormat("<input type=\"hidden\" name=\"{0}\" value=\"{1}\">", name, value);
    }

    private void PayPalPostScriptLONG(Page page)
    {

        HttpContext.Current.Response.Write(GetForm());//add form to page

        StringBuilder strScript = new StringBuilder();
        strScript.Append("<script language='javascript'>");
        if (UserID == string.Empty || EventID == string.Empty || TransactionType == string.Empty)
        {
            strScript.Append("alert('missing event, user, or transactiontype');");
        }
        strScript.Append("var ctlForm = document.getElementById('frmPP');");
        strScript.Append("ctlForm.submit();");
        strScript.Append("</script>");
        page.ClientScript.RegisterClientScriptBlock(typeof(Page), "PPSubmit", strScript.ToString());

    }


Dont do it this way. Write your form out as html text to your response then append a jscript function that finds it and submits it.

Like so:

Response.Write(<Form...);
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var daForm = document.getElementById('frmPP');");
strScript.Append("daForm.submit();");
strScript.Append("</script>");
ClientScript.RegisterClientScriptBlock(typeof(Page), "PPSubmit", strScript.ToString());


Use <script type="text/javascript"> !!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜