0

I'm having trouble to append multiple inputs. I am reading a list with items and values and want to submit them over Javascript (POST), but I can't figure out why this doesn't want to work. I tried in several ways, finally I came up with this, but it doensn't want to iterate over it and throw an error:

var form = document.createElement("form");
form.action = "submitform.php";
form.method = "post";
form.target = "_blank";
input = [];

var kvarray = document.getElementsByClassName('ua_mattext');

    for (var i = 0; i < kvarray.length; i++) {

        var fieldname = kvarray[i].id;
        var fieldvalue = kvarray[i].value;

        input[i] = "input_" + i;
        document.createElement(input[i]);
        input[i].type = "hidden";
        input[i].name = fieldname;
        input[i].value = fieldvalue;
        form.appendChild(input[i]);
    }
    document.body.appendChild(form);
    form.submit();

What am I doing wrong?

5
  • brusharr.length???? and document.createElement(input[i]); is doing nothing. It does not magically update the array with an input and there is no element with the type <input_1/> Commented Nov 3, 2016 at 19:07
  • kvarray is going to return a node list (an array-like object containing element nodes). kvarray[i] will reference a given element node - - not a name. Commented Nov 3, 2016 at 19:08
  • 1st: you are iterating over brusharr instead of kvarray; why?? 2nd:what you pretend to be fieldname is a node object, not a text; 3rd: input[i] is a text, not an input. Review that and rewrite your question, please. Commented Nov 3, 2016 at 19:08
  • Oh sorry, I forgot ... brusharr is the same as kvarray, I renamed half of it in SO... Sorry - Corrected myself in the edited post Commented Nov 3, 2016 at 19:16
  • @sergio0983 Corrected myself in the question and also answered it, since I figured it out. Thanks everyone for their help. Commented Nov 4, 2016 at 9:59

2 Answers 2

1

I figured it out by myself. Firstly, I wasn't declaring the input variable properly (Forgot the "var " at the beginning)

Secondly, I wasn't setting the document.createElement() to the input.

Thirdly, I was naming the input input_1,input_2, which was invalid. There is only one, which is input.

So, now it's working. Below is the corrected code:

var form = document.createElement("form");
form.action = "submitform.php";
form.method = "post";
form.target = "_blank";
var input = [];

var kvarray = document.getElementsByClassName('ua_mattext');

    for (var i = 0; i < kvarray.length; i++) {

        var fieldname = kvarray[i].id;
        var fieldvalue = kvarray[i].value;

        input[i] = "input_" + i;
        input[i] = document.createElement("input");
        input[i].type = "hidden";
        input[i].name = fieldname;
        input[i].value = fieldvalue;
        form.appendChild(input[i]);
    }
    document.body.appendChild(form);
    form.submit();
Sign up to request clarification or add additional context in comments.

Comments

0

When you say

  input[i] = "input_" + i;

nothing about this line actually creates an input HTML element. Instead, you can do something like this

  input[i] = <input type="hidden" name="${fieldname}" value="${fieldvalue}">

which would also have the added benefit of replacing the 3 lines following that one.

p.s: you should also declare input in the beginning by saying:

var input = []

p.p.s: You should check your assumptions as to what kvarray[i] is equal to. If you are doing kvarray[i].value in the subsequent line, I doubt that kvarray[i] is going to be a string, which is what you will need it to be in order to set the input's name attribute equal to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.