With select2 v.4.0 you can use several drop-down menus.
Set the name as choose_usr_email[] so that it creates an array of tags to send.
HTML
<form action="" id="tagForm"> <select multiple="true" name="choose_usr_email[]" id="choose_usr_email" class="form-control select2"> </select> <button type="submit">Submit</button> </form>
Script
$('#choose_usr_email').select2({ tags: true, // automatically creates tag when user hit space or comma: tokenSeparators: [",", " "], ajax: { url: "time.php", dataType: 'json', //search term data: function (term, page) { return { q: term, // search term page: page }; }, results: function (data, page) { return { results: data}; } } }); // handle form submission: $('#tagForm').submit(function(e){ e.preventDefault(); $.ajax({ // PHP file where you send selected values: url : "file.php", // if you want to use $_POST in PHP, uncomment the line below: // type : 'POST', dataType : 'json', // serialize the form: data : $('#tagForm').serialize(), success : function(response){ // handle server response ... } }); });
PHP (file where you send the selected values)
<?php // If 'type' is not specified in AJAX, use $_GET // check if 'choose_usr_email' exists in AJAX request if(isset($_GET['choose_usr_email']){ // if exists, loop through the values: foreach($_GET['choose_usr_email'] as $key => $value){ // do something with each $value (each submitted tag) } } ?>
Demo
For Select2 <v.4.0
$('#choose_usr_email').val(); returns each selected tag identifier (if specified) or text separated by a coma (1,2,3, ...).
$('#tagForm').submit(function(e){ e.preventDefault(); $.ajax({ // PHP file where you send selected values: url : "file.php", // if you want to use $_POST in PHP, uncomment the line below: // type : 'POST', dataType : 'json', // request data, split input field value by comma: data : { choose_usr_email : $('#choose_usr_email').val().split(',') }, success : function(response){ // handle server response ... } }); });
You can then handle the AJAX request in the same way as in the previous PHP example.