0

I have a JavaScript function to send two POST parameters, one variable and one hardcoded. However, when this function sends POST parameters, actually it sends only the first parameter and not the second. I can't figure out why is this happening The function code is :

function post_to_url(path, params, method) {

    method = method || "post"; // Set method to post by default if not specified.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "sortvalue");
    hiddenField.setAttribute("value", params);

    var hiddenFieldMode = document.createElement("input2");
    hiddenFieldMode.setAttribute("type", "hidden");
    hiddenFieldMode.setAttribute("name", "mode");
    hiddenFieldMode.setAttribute("value", "showmain");
    form.appendChild(hiddenField);
    form.appendChild(hiddenFieldMode);
    document.body.appendChild(form);
    form.submit();

}

I saw the POST data and it is always sortvalue=givenvalue and not sortvalue=givenvalue&mode=showmain as expected..

2 Answers 2

2

Because you created an <input2> element.

var hiddenFieldMode = document.createElement("input2");

Change it to:

var hiddenFieldMode = document.createElement("input");
Sign up to request clarification or add additional context in comments.

Comments

1

You're creating an element with the tag name "input2". That's not going to be interpreted as an <input> element.

There's no need to use .setAttribute() (in any browser) to set the properties of your new DOM nodes:

var hiddenFieldMode = document.createElement("input");
hiddenFieldMode.type = "hidden";
hiddenFieldMode.name = "mode";
hiddenFieldMode.value = "showmain";

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.