3

I would like to POST a form with attributes as described here (MVC friendly array): https://stackoverflow.com/a/5003686/1246219 The reason I want to POST a form rather than using ajax is that the result is a file for download, i.e.there is no other reason for using a form.

However I have a javascript array containing the values (integers) that I would like to submit. The only ways to achieve this that I can think of are either to loop through and create input elements for each entry, or I could just populate a single form field with a comma delimited string and parse it server side. Is there a better way?

UPDATE - If anyone is interested I have ended up with the loop approach, and happened to write it with d3 instead.

var selected = [1,2,3];
var selectionBinding = d3.select("#myForm").selectAll("input[name='myField']").data(selected);
selectionBinding.exit().remove();
selectionBinding.enter().append("input")
                .attr("type", "hidden")
                .attr("name", "myField")
                .attr("value", function (data) { return data });
$("#myForm").submit();

1 Answer 1

4

My suggestion would be to create a regular Javascript Array with data and have a form with input type text. When you submit the form set the value of the input text as JSON.stringify(myArray)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks closure that is a good idea. In the end Ive actually gone the other way with a loop

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.