-1

I am trying to process a form through POST method and i am having trouble with passing multiple selection field.

<label for="sport">Favourite sport: </label>
    <select id="sport" name="favsport[]" size="4" multiple>
        <option value="soccer">Soccer</option>
        <option value="cricket">Cricket</option>
        <option value="squash">Squash</option>
        <option value="golf">Golf</option>
        <option value="tennis">Tennis</option>
        <option value="basketball">Basketball</option>
        <option value="baseball">Baseball</option>
    </select>





$sportstr = $_POST["favsport"];
<p><strong> Favourite Sport = </strong> <?php echo "$sportstr"; ?></p>
Raveel
  • 9
  • 3
  • `print_r($sportstr);` will reveal you are trying to echo an array...maybe a loop or an `implode()` is in order? – Rasclatt Apr 26 '17 at 05:03
  • Possible duplicate of [Get $\_POST from multiple checkboxes](http://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes) – S4NDM4N Apr 26 '17 at 05:18

3 Answers3

0

It is because your field name is favsport[] and it give you array in php $_POST["favsport"] you are doing echo $sportstr variable it will give you Notice: Array to string conversion thats why you have to use print_r() function to print array. use below code it might be your solution.

$sportstr = $_POST["favsport"];
print_r($sportstr);
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
0

When you post the form with the multiple selection field favsport[] it is posted as an array:

array (
  'favsport' => 
  array (
    0 => 'cricket',
    1 => 'squash',
    2 => 'golf',
    3 => 'tennis',
    4 => 'basketball',
    5 => 'baseball',
  ),
)
Zeusarm
  • 1,038
  • 6
  • 14
0

When you are dealing with multiselect dropdown, then you can get its value like:

$sportstr = $_POST["favsport"];

here $sportstr is an array, so you have to use foreach() to access its all elements.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59