Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select field in form is not sending data using php

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>
like image 514
arboles Avatar asked Jan 17 '23 19:01

arboles


2 Answers

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>
like image 61
Petah Avatar answered Jan 25 '23 14:01

Petah


I know you got your answer, just some more information for those who face the same problem but that solution did not work:
I had the same problem and it turned to be made by Bootstrap!
In case you are using Bootstrap , you need to provide class attribute of select:

<select name="any_name" class="form-control">
    <option value="this is what would be sent if selected"> sth </option>
</select>


for more information take a glance at this discussion too!

like image 32
Shahryar Saljoughi Avatar answered Jan 25 '23 14:01

Shahryar Saljoughi