creating and submitting a form with javascript

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, pw) {
var form = document.createElement("form");
document.body.appendChild(form);
form.method = "POST";
form.action = "login.php";
var element1 = document.createElement("<INPUT NAME='un' TYPE='hidden' VALUE='"+un+"'>");  
 form.appendChild(element1);
var element2 = document.createElement("<INPUT NAME='pw' TYPE='hidden' VALUE='"+pw+"'>"); 
form.appendChild(element2);
form.submit();
}

Answers:

Answer

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();
}
Answer

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();
}
Answer

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();
Answer

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");

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.