transmitem POST fara forma

search-form-post De multe ori avem nevoie sa transmitem
unele date prin metoda POST de pe o
pagina web la alta (sau la o adresa) ,
de fiecare data trebuie sa scrim

<form method="post" action=".....">
<input ................................
</form>

Ce sa facem atunci cand transmitem dintr-un JavaScript fisier? si mai mult – trebuie automatizat procesul, fara desenarea formelor , butonului “submit”, etc…

Am realizat urmatoarea functie, este nevoie doar de a o apela cu parametrii doriti

postToUrl(‘/adresa/fisier.php’,  {json1:”data1″, json2:”data2″ }, 1);

Scriptul

// initializam elementele DOM
// my_createElement("nume_tag", {"nume_attribute":"value_attribute", "nume_attribute_2":"value_attribute_2"})

var my_createElement = (function(){
    //verificam browser-ul IE(http://habrahabr.ru/post/50544/)
    var IE='\v'=='v';
    if (IE){
        var attrTranslations ={
            "class": "className",
            "for": "htmlFor"
        };
        var setAttribute = function(element, attr, value){
            if (attrTranslations.hasOwnProperty(attr)){
                element[attrTranslations[attr]] = value;
            }else if (attr == "style"){
                element.style.cssText = value;
            }else{
                element.setAttribute(attr, value);
            }
        };

        return function(tagName, attributes){
            attributes = attributes || {};
            // http://channel9.msdn.com/Wiki/InternetExplorerProgrammingBugs
            if (attributes.hasOwnProperty("name") ||  attributes.hasOwnProperty("checked") ||  attributes.hasOwnProperty("multiple")){
                var tagParts = ["<" + tagName];
                if (attributes.hasOwnProperty("name")){
                    tagParts[tagParts.length] = ' name="' + attributes.name + '"';
                    delete attributes.name;
                }if (attributes.hasOwnProperty("checked") && "" + attributes.checked == "true"){
                    tagParts[tagParts.length] = " checked";
                    delete attributes.checked;
                }if (attributes.hasOwnProperty("multiple") && "" + attributes.multiple == "true"){
                    tagParts[tagParts.length] = " multiple";
                    delete attributes.multiple;
                }
                tagParts[tagParts.length] = ">";

                var element = document.createElement(tagParts.join(""));
            }  else {
                var element = document.createElement(tagName);
            }

            for (var attr in attributes) {
                if (attributes.hasOwnProperty(attr)) {
                    setAttribute(element, attr, attributes[attr]);
                }
            }

            return element;
        };
    }
    //restul browserelor
    else{
        return function(tagName, attributes){
            attributes = attributes || {};
            var element = document.createElement(tagName);
            for (var attr in attributes) {
                if (attributes.hasOwnProperty(attr)){
                    element.setAttribute(attr, attributes[attr]);
                }
            }
            return element;
        };
    }
})();

function postToURL(url, values, new_win) {
    values = values || {};
    new_win = new_win || 0;

    var form = my_createElement("form", {action: url,
        method: "POST",
        style: "display: none",
        target: new_win == 0 ? '' :"_blank"});
    for (var property in values) {
        if (values.hasOwnProperty(property)) {
            var value = values[property];
            if (value instanceof Array) {
                for (var i = 0, l = value.length; i < l; i++) {
                    form.appendChild(my_createElement("input", {type: "hidden",
                        name: property,
                        value: value[i]}));
                }
            }else {
                form.appendChild(my_createElement("input", {type: "hidden",
                    name: property,
                    value: value}));
            }
        }
    }
    document.body.appendChild(form);
    //console.log(form);
    form.submit();
    document.body.removeChild(form);
}