-3

Using Javascript I have created an array with elements.

Now, I want to assign that array into HTML hidden input field to use this input field as form input field.

When I submitted the form, this should go to the controller as an array.

Javascript:

  var schools= [];

 var address1    = place.name;

 var latitude1   = place.geometry.location.lat();

 var longtitude1 = place.geometry.location.lng();


 schools.push([address1,latitude1,longtitude1]);

 document.getElementById("addSchools").value = schools;

HTML:

<input type="hidden" class="form-control" placeholder="Search Box" 

id="addSchools" name="addSchools[]" value="">
Gayalss
  • 5
  • 1
  • 6

1 Answers1

0

There are two ways for the assigning array to an input field in HTML. 1. Convert array to Json string and put in input as to value (It can use in some special cases. Please check if it can work in your case.). 2. use multiple columns with array notation in the name of input field. like:-

<input type="hidden" class="form-control" placeholder="Search Box" name="addSchools[]" value="array[0]"> <input type="hidden" class="form-control" placeholder="Search Box" name="addSchools[]" value="array[1]"> <input type="hidden" class="form-control" placeholder="Search Box" name="addSchools[]" value="array[2]">

It will provide an array of addSchools[] in form data.

Sahil Gupta
  • 578
  • 5
  • 16