Jaylen
Jaylen

Reputation: 40289

How to post values from multiple drop down menus in the same form

I have a form with a unique identifier for each row. I also have a drop down menu with user names.

basically each row has a task and next to each task a drop down menu with name of the task owner. what I need to do is to be able to POST the users name along with their task id so I can update the owner of the task in one submit.

let say the following is what I have in my form

    ID    Title        MENU
    1     Task 1       (please select)
    2     Task 2       (please select)
    3     Task 3       (please select)
make_change

if the user selected "Mike" in row 1 then I want to update the database with Mike's ID which is Mike's option value in the drop down menu.

The thing that I am having a difficulty with in trying to link each menu to the correct row id "task id". this is how my menu code is displayed

<select name="user_id[]">
<option value="200">Mike</option>
<option value="205">Jaylen</option>
<option value="220"> Jack</option>
</select>

I want to be able to link each menu to the correct task id so when I post the form I can link each menu to the correct row.

How can I include the task id in each menu?

Thanks

Upvotes: 0

Views: 1866

Answers (2)

Steven
Steven

Reputation: 6148

So, my understanding is that you need to know which Task ID is related to which select?

Assuming that you create the selects in PHP you could simply change

<select name="user_id[]">

to

<select name="user_id[TASK_ID]">

where TASK_ID is the actual id for the task... (e.g. 1, 2, or 3 etc).

Then when you submit to PHP you could loop through your $_POST[user_id] with for example a foreach loop...

foreach($_POST['user_id'] as $taskid => $userid){
    //Do something with the information....
}

UPDATE

If you know the TASK_ID you don't need to iterate through the entire post you can simply access the required field using: $_POST['user_id'][TASK_ID]

For example...

echo $_POST['user_id'][1]; //Echo's the user id for task 1
echo $_POST['user_id'][8]; //Echo's the user id for task 8

Upvotes: 1

black hole
black hole

Reputation: 256

Instead of making the name for the select tag an array you can make them unique names. This way each row can be uniquely identified. Example:

<select name="task1">
    <option value="200">Mike</option>
    <option value="205">Jaylen</option>
    <option value="220"> Jack</option>
</select>

<select name="task2">
    <option value="200">Mike</option>
    <option value="205">Jaylen</option>
    <option value="220"> Jack</option>
</select>

This way task1 will map to whatever is selected in the dropdown for task1 and the same for the task2 dropdown.

Upvotes: 0

Related Questions