Domanda

Can anyone tell me how to get the 'value' of selected items from a multivalued select?

I have the following:

    <select id="category" name="category[]" multiple="multiple">';

        /* Select categories */ 
        [...]
        <option value"' . $row->id . '">' . $row->name . '</option>';

which would return:

                    <option value"1">itemName1</option>

Why is the code below giving me the selected text and not their values? What's wrong here?

                    $category = $_POST['category'];
        if (count($category) > 0){ 

            foreach ($category as $key => $value) {
                echo  $value . "<br>\n";
            }
        }

This is returning itemName1 and I need the actual value (1)

Thank you

È stato utile?

Soluzione

Your HTML is invalid. You have:

<option value"1">itemName1</option>

while it should be:

<option value="1">itemName1</option>

Altri suggerimenti

mulple select will return a query like this (name = name and values = int)

name=1&name=4&name=99

so it return the selected values the array would look like:

array(
    name => array(
        [0] => 1
        [1] => 4
        [2] => 99
    )
)

ps if you fix the HTML error, you will most likely get a valid result

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top