Get all Selected values ββof Select list via Submit form - Jquery Chosen
I am using JQuery Chosen Plugin.
I have this MultiSelect list on my page.
<select name="articles_on_this_menu" multiple="multiple" data-placeholder="Choose Articles" id="articles_on_this_menu">
<option value="27" selected="selected">Join Team</option>
<option value="11">Faislabad</option>
<option value="12">Peshawar</option>
<option value="28" selected="selected">Track your Order</option>
<option value="34" selected="selected">AboutUS2</option>
</select>
I want to save these SELECTED values ββby POST via PROM submit.
But when submitted, I only get the last selected value from the list.
I know that I can get all selected values ββvia JQuery
var articles_on_this_menu= $("#articles_on_this_menu").val()
But I want to get into FORM submit .... If I do articles_on_this_menu[]
then I don't get the MultiSelect list in Chosen Plugin
way
EDIT: People are posting answers in JS or JQuery solutions, but I already said that I don't want to use JS or JQuery. I want to get all selected values ββin an array when submitting a form
I hope this is what you are looking for, I have the same problem but for a different reason.
var data = {'cat_ids[]' : []};
$('select').find('option:selected').each(function(){
data['cat_ids[]'].push($(this).val());
});
var posting = $.post("you url here",data);
posting.done(function( data ){
$('ID/Class').empty().append(data);
});
in your php file you can get content
print_r($_POST['cat_ids']);
source to share
Use below code - use : selected - selector
var selectedValueArray= [];
$('#articles_on_this_menu :selected').each(function(i, selected){
selectedValueArray[i] = $(selected).text();
});
source to share
Edited HTML code:
<select name="articles_on_this_menu[]" multiple="multiple" data-placeholder="Choose Articles" id="articles_on_this_menu">
<option value="27" selected="selected">Join Team</option>
<option value="11">Faislabad</option>
<option value="12">Peshawar</option>
<option value="28" selected="selected">Track your Order</option>
<option value="34" selected="selected">AboutUS2</option>
</select>
Use the multi-select name as an array name = "articles_on_this_menu []" Then you will get an array of the selected options when submitting the form.
PHP code:
//Array of selected articles
$selectArticles = isset($_POST['articles_on_this_menu'])
? $_POST['articles_on_this_menu']
: array();
source to share