Adding form on the fly and submitting

Posted on: October 1, 2010 Posted by: John Huh Comments: 0

Adding form on the fly and submitting

[code lang=”js”]
function post_to_url(path,params,method){
method=method||"post";
var form=document.createElement("form");
form.setAttribute("method",method);
form.setAttribute("action",path);
for(var key in params){
var hiddenField=document.createElement("input");
hiddenField.setAttribute("type","hidden");
hiddenField.setAttribute("name",key);
hiddenField.setAttribute("value",params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form.submit();
}
[/code]

Consume it: <a title=”Title” href=”javascript:post_to_url(‘http://www.domain.com’,{‘rsit’:’3528′,’sit’:’2726′})”>Link</a>

Leave a Reply:

Your email address will not be published. Required fields are marked *