Jimmy
Jimmy

Reputation: 2915

PHP POST not receiving selected options

I'm trying to post a simple select with the multiple attribute:

<select name="cboCategory[]" id="cboCategory" class="box" multiple="multiple" size="5">
    <option value="18">Cookies</option>
    <option value="19">Cookie Bouquets</option>
    <option value="20">Cookie Platters</option>
    <option value="21">Cookie Pizzas</option>
    <option value="22">Cakes</option>
    <option value="23">Cupcakes</option>
    <option value="25">Gifts - Corporate</option>
    <option value="26">Gifts - Misc.</option>
    <option value="27">• Birthday</option>
    <option value="28">• Baby</option>
    <option value="29">• Get Well</option>
    <option value="30">• Congratulations</option>
    <option value="31">• Thank You</option>
    <option value="35">• Holiday</option>
    <option value="41">Gifts</option>
    <option value="42">&nbsp;&nbsp;Corporate</option>
    <option value="43">&nbsp;&nbsp;Miscellaneous</option>
</select>

On the PHP side, I'm just trying to print out the selected values:

var_dump( $_POST['cboCategory'] ); exit;

When I have it set up this way, the output is always string(0) "". It just won't pass any of the selected options.

However, if I remove the brackets from the select name, changing it to name="cboCategory" it works fine, returning only one of the selected values.

Can anyone else figure out why I can't get this to work? I feel like I must be missing something obvious.

EDIT: Here's the entire $_POST variable:

array(15) { ["cboCategory"]=> string(0) "" ["txtName"]=> string(28) "50th Birthday Cookie Bouquet" ["mtxDescription"]=> string(266) "Half a decade is a big deal! Celebrate it with one of our cookie bouquets. They are overflowing with our delicious homemade large cookies on a stick, balloon on a stick, large candy bar, candy bites, ribbons, decorations, and your enclosure card. Container may vary." ["txtOptionName"]=> string(7) "default" ["txtValue1"]=> string(9) "4 cookies" ["txtPrice1"]=> string(2) "25" ["txtValue2"]=> string(9) "6 cookies" ["txtPrice2"]=> string(2) "30" ["txtValue3"]=> string(9) "8 cookies" ["txtPrice3"]=> string(2) "35" ["txtValue4"]=> string(0) "" ["txtPrice4"]=> string(0) "" ["txtValue5"]=> string(0) "" ["txtPrice5"]=> string(0) "" ["txtQty"]=> string(4) "4999" }

EDIT2: Thanks for all the help everyone. I found the issue, it was indeed outside of what I had posted. I had my var_dump at the top of the page, and the post was working fine without the brackets, so I had assumed everything else was set up correctly. However, there were a couple includes before my var_dump, and there was something in one of the includes ruining arrays in $_POST.

Upvotes: 0

Views: 929

Answers (3)

user2333596
user2333596

Reputation:

in php

foreach($_POST['cboCategory'] as $category)
{

echo $category . "<br>";

}

what christian said is not right

Sure you're not using get instead of post on the form's method attribute?

Upvotes: 0

raidenace
raidenace

Reputation: 12826

There is no issue in your code. You should ensure that you have placed your select box inside the form tag, that at least one element is selected and that there are no other elements in the form tag with the same name.

Checklist:

  1. Select box is inside form tag
  2. Does not have another element with same name inside same form
  3. At least one element is selected in select box

Upvotes: 1

brunoais
brunoais

Reputation: 6836

Is that selectBox inside the form you are submitting?

Is that form submitting with action="post" ?

Upvotes: 0

Related Questions