With the select2 jQuery PlugIn I created a tag box. Therefore I used the following html form code:
<form action="someAction" method="post">
<fieldset>
<select id="tags" multiple="" name="tags">
<option value="tag1">val1</option>
<option value="tag2">val2</option>
<option value="tag3">val3</option>
</select>
<button type="submit" class="btn">Submit</button>
</fieldset>
</form>
Select2 generates the following field from it:
<input type="text" class="select2-input" autocomplete="off" id="s2id_autogen1" style="width: 10px;">
The problem is here that the name
attribute is missing. How can I get the data/text of the input field out of the $_POST
variable in PHP if there is no name
attribute? How can I manage this?
The setup JS is the following:
$(document).ready(function() {
$("#tags").select2({
placeholder: "Insert tag",
allowClear: true,
minimumInputLength: 2,
maximumSelectionSize: 1,
minimumWidth: 200
});
});
Definetly I solved that with this simple snippet.
I added a hidden input:
<input type="hidden" id="manufacturer" name="manufacturer" value="1">
And an ID in the select:
<select class="full-width required" data-placeholder="Select Country" data-init-plugin="select2" required id='myselect'>
Then just add that simple JS in the same page to change the hidden value every time the user change the value:
<script type='text/javascript'>
$(function() {
$('#myselect').change(function() {
// if changed to, for example, the last option, then
// $(this).find('option:selected').text() == D
// $(this).val() == 4
// get whatever value you want into a variable
var x = $(this).val();
// and update the hidden input's value
$('#manufacturer').val(x);
alert("cambiado");
});
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With