dynamically render select options in a form
hi everyone
i want to dynamically render select options in a form, the number of the select tags that are rendered will depend on how many a user has selected..
for example, if a user says they want 3 items. i want to display 3 select options tags. Each of the input tags will have their values fed to them from an array function(its just a simple php array).
i am not sure of the best way to dynamically render these tags and would really appreciate some advice.
it might help if i show u what i mean.
the array values to be fed to the select tag:
-
arrayValues() { return $selection = array( '0' => 'none' ,' 1' => '1 item' ,' 2' => '2 item' ,' 3' =>' 3 item' ); }
the select tag
-
<?php echo ' <select id="howmany" name="items[howmany]" />'; foreach ($arrayValues as $key => $value) { echo '<option value="' . $key .'">' . $value . '</option>'; } echo'</select>'; ?>
the code to determine how many tags a user wants;
-
$(".howmany").change(function(){ var value = $(this).val(); }
i know how to dynamically render imput tags for a simple imput bar. i.e
-
$('button').click(function () { $('div').empty().append(new Array(+$('input[type=number]').val()+1) .join("<input type='text' placeholder='Type Something'/>")); });
the issue for me is that my select tags has values fed to it from the $arrayValues
.
thank you in advance for your kind assistance