Jaylen
Jaylen

Reputation: 40301

how can I get all options in a multi-options select using PHP?

I am using 2 select menus where one have value and the other one is empty and when a user select items from the left and click a bitton the selected items are moved to the right menu. a similler example is on this page http://jquerybyexample.blogspot.com/2012/05/how-to-move-items-between-listbox-using.html

the only problem is when i do a $_POST['lstBox2 '] I only get the selected value. But in here I want to $_POST all option weather they are selected or not.

I have done this jquery code that does that but if a used clicked on one option by mistake then php will try to post only one

$("#lstBox2 option").each(function() {
    $(this).attr('selected', 'selected');
});

can someone tell me if there a way in php to always $_POST all they value?

thanks

Upvotes: 0

Views: 411

Answers (2)

zhujy_8833
zhujy_8833

Reputation: 571

You can send the information of selected options in JSON format, and in server-side, you can easily use PHP json_decode() to parse the information.

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191749

Just move the JavaScript code in your question to the submit callback of your form.

$("form:has(#lstBox2)").on('submit', function () {
    $("#lstBox2 option").prop('selected', true);
});

Upvotes: 2

Related Questions