Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2 does not create name attribute

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 $_POSTvariable 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
});

});

like image 373
tester Avatar asked Nov 12 '22 06:11

tester


1 Answers

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>
like image 168
Dani Polo Avatar answered Nov 14 '22 23:11

Dani Polo