select field in form is not sending data using php

forms, php, post, select

Solution

You need to put the name attribute on the `select` tag, not the `option` tag.

<select name="user_page_id">
    <?php foreach ($dis as $key => $list): ?>
        <option value="...">...</option>
    <?php endforeach; ?>
</select>

Problem

so when i send the form with with the first option 'public' selected. the data is inserted. but when i try submitting the form with the other option selected, the ones in the for each loop. the data no longer is sent. i have inspected the elements. and they are outputting all of the correct values. and they are displaying properly. why arent they inserting into the db? when i click submit nothing happens. but when i click submit for the first option, it works fine? ``` <form method='POST' action='add.php'> <select> <option name="user_page_id" value="<?php echo $_SESSION['user_id']; ?>">Public</option> <?php $dis=show_groups_select_list($_SESSION['user_id']); foreach($dis as $key=>$list){ echo '<option name="user_page_id" value="'.$list['id'].'">'.$list['username'].'</option>'; } ?> </select> </form> ```

Original source