Friday, November 14, 2008

Create a html form on the fly and submit using javascript

Scenario:
I had to plug in a UPS tracking form. There's difficulties putting a HTML form inside an asp.net form tag. Also as the data to be passed in the form was coming from the database it didn't make sense to load the data in a form and have the user press the submit button. Although I'm still submitting a form. To the user it seems like they're simply clicking on a hyperlink.

HTML


<asp:HyperLink ID="hl_track" runat="server"
NavigateUrl='<%# getJs(Eval("Tracking").ToString()) %>'
Text='<%# Eval("Tracking") %>' Font-Overline="False" Font-Underline="True"></asp:HyperLink>




C#

Return the navigation URL as so;

protected string getJs(string tracking)
{
string ups_from = string.Format(@"<form name=""ups"" id=""ups"" method=""post"" action=""http://wwwapps.ups.com/WebTracking/OnlineTool"" target=""_blank""><input type=""text"" size=""35"" name=""InquiryNumber1"" value=""{0}"" /><input type=""hidden"" name=""UPS_HTML_License"" value=""3C34A55003D153B0"" /><input type=""hidden"" name=""IATA"" value=""us"" /><input type=""hidden"" name=""Lang"" value=""eng"" /><input type=""hidden"" name=""UPS_HTML_Version"" value=""3.0"" /><input type=""hidden"" name=""TypeOfInquiryNumber"" value=""T"" /></form>", tracking);

string js = string.Format("javascript:submitUPS('" + ups_from + "','ups');");

return js;
}



JavaScript
function submitUPS(form_content, form_name) {
var _body = document.getElementsByTagName('body')[0];
var _div = document.createElement('div');
_body.appendChild(_div);

_div.innerHTML = (form_content);

document.getElementById(form_name).submit();
}

2 comments:

Anonymous said...

Приглашаем Вас посетить нашу [url=http://luxdoska.com]доску бесплатных объявлений[/url]. Вы сможете оставить свое объявление без регистрации, но с обязательной постмодерацией.
Надеемся что на нашей доске вы найдете именно то что вам нужно, или же сможете выгодно продать свои услуги

Anonymous said...

Новый сайт видео [url=http://videolov.com]знакомства, [/url]регистрация бесплатна.