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

+3


source to share


4 answers


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']);

      

0


source


Use below code - use : selected - selector



var selectedValueArray= []; 
$('#articles_on_this_menu :selected').each(function(i, selected){ 
  selectedValueArray[i] = $(selected).text(); 
});

      

0


source


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();

      

0


source


Usage articles_on_this_menu[]

in html works great in PHP.

If you are using python in the backend use this: (no need to change html) request.POST.getlist('articles_on_this_menu')

for Django and request.form.getlist('articles_on_this_menu')

for cases respectively.

0


source







All Articles