creating and submitting a form with javascript

前端 未结 4 493
春和景丽
春和景丽 2020-12-05 06:55

I\'m trying to create a form and submit it immediately with javascript and I cannot figure out what I\'m doing wrong. here is my code:

function autoLogIn(un,         


        
相关标签:
4条回答
  • 2020-12-05 07:46

    I've created a function to do this:

    function PostDataRedirect(data, dataName, location) {
    var form = document.createElement("form");
    
    form.method = "POST";
    form.action = location;
    
    if (data.constructor === Array && dataName.constructor === Array) {
        for (var i = 0; i < data.length; i++) {
            var element = document.createElement("input");
            element.type = "hidden";
            element.value = data[i];
            element.name = dataName[i];
            form.appendChild(element);
        }
    } else {
        var element1 = document.createElement("input");
        element1.type = "hidden";
        element1.value = data;
        element1.name = dataName;
        form.appendChild(element1);
    }
    
    document.body.appendChild(form);
    
    form.submit(); 
    }
    

    Usage for question:

    PostDataRedirect([ un, pw ], ["un", "pw"], "login.php");
    
    0 讨论(0)
  • 2020-12-05 07:48

    Modified code: jsfiddle.

    function autoLogIn(un, pw) {
       var form = document.createElement("form");
       document.body.appendChild(form);
       form.method = "POST";
       form.action = "login.php";
       var element1 = document.createElement("INPUT");         
        element1.name="un"
        element1.value = un;
        element1.type = 'hidden'
        form.appendChild(element1);
        var element2 = document.createElement("INPUT");         
        element2.name="pw"
        element2.value = pw;
        element2.type = 'hidden'
        form.appendChild(element2);
        form.submit();
    }
    
    0 讨论(0)
  • 2020-12-05 07:50

    You also can do the Saurabh Chauhan response but without add the dinamic element to body This solution is all dinamic solution.

        var myform = document.createElement("form");
        myform.action = "myForm.aspx";
        myform.method = "post";
    
    
        product = document.createElement("input");
        product.value = "value";
        product.name = "name";
    
        myform.appendChild(product);
        myform.submit();
    
    0 讨论(0)
  • 2020-12-05 07:55

    The problem is that createElement does not accept HTML. It accepts a tagname and returns a DOM element. You can then set the value attribute on this element to what you require.

    function autoLogIn(un, pw) {
        var form = document.createElement("form");
        var element1 = document.createElement("input"); 
        var element2 = document.createElement("input");  
    
        form.method = "POST";
        form.action = "login.php";   
    
        element1.value=un;
        element1.name="un";
        form.appendChild(element1);  
    
        element2.value=pw;
        element2.name="pw";
        form.appendChild(element2);
    
        document.body.appendChild(form);
    
        form.submit();
    }
    
    0 讨论(0)
提交回复
热议问题