I have an array that I need to submit to a form. An array does not fit into any of the known form elements. How can I fit this array into scheme of forms and submit it?
Printable View
I have an array that I need to submit to a form. An array does not fit into any of the known form elements. How can I fit this array into scheme of forms and submit it?
arrays on forms work like this (if submitted via post, otherwise use $_GET):
so, if you have your array, you can simply loop through it using a foreach(), and then put it into hidden elements.PHP Code://the following produces the variable $_POST['array']['key'] with a value of "test"
<input type="hidden" name="array[key]" value="test" />
//the following produces the variable $_POST['array'][0] through $_POST['array'][2] with a value of "test"
<input type="hidden" name="array[]" value="test" />
<input type="hidden" name="array[]" value="test" />
<input type="hidden" name="array[]" value="test" />
the above code would produce an array that could be accessed via the same keys as the original array, but the "root" array would be $_POST['myarray'] rather than $myarray. for simplicity's sake, you could redefine it to use $myarray after you had submitted the form.PHP Code:<?php foreach($myarray as $key => $value): ?>
<input type="hidden" name="myarray[<?php echo $key; ?>]" value="<?php echo htmlentities($value); ?>" />
<?php endforeach; ?>
hope that was what you were looking for!
Yes, this is what I was looking for. Thanks!:D