dl_dx
dl_dx

Reputation: 1

Can PHP handle $_POSTing a multidimensional array with another field in the same form as a key?

So I have a html form with a select field called "user_id[]", and another select field in the same form that is called "equipment_id[]. Essential points:

what I currently get when I submit the form when I var dump an example entry for two users is:

["user_id"]=> array(2) { 
        [0]=> string(3) "178" 
        [1]=> string(3) "181" 
} 

["equipment_id"]=> array(5) { 
        [0]=> string(1) "3" // this element was selected with user_id 178
        [1]=> string(1) "4" // this element was selected with user_id 178 
        [2]=> string(1) "3" // this element was selected with user_id 181
        [3]=> string(1) "4" // this element was selected with user_id 181 
        [4]=> string(1) "5" // this element was selected with user_id 181 
}  

What I'd like to get to is a situation where the user_id has each of the selected equipment_ids under it, rather than in a single array so I can associate the equipment selected with the user selected with it.

I thought this might be achieved by using the "user_id[]" field as a key for the "equipment_id[]" field so that if the form is submitted, the equipment might be associated with the user.

form elements are:

<select name="user_id[]">
    <option value="" disabled selected>Choose a User</option>
    <option value="178">178</option>
    <option value="181">181</option>
</select>

<select name="equipment_id[]" multiple tabindex="10">
    <option value="" disabled selected>Choose Equipment</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

So questions:

Any pointers gratefully received! :0)

UPDATE

I think I have stumbled onto the answer to this question during some other Stackexchange research.

My code is below

PHP/HTML

<?php
    var_dump($_POST);
?>
<html lang="en">
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <button id="repeat">Add New Items</button>   
        <form class="work_entry_form" id="workform" action="#" method="POST">   
        <div class="group">
            <select name="user_id[0]" id="user_id">
                  <option value="" disabled selected>Choose user</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="1">3</option>
                  <option value="2">4</option>
            </select>
            <select name="equipment_id[0][]" id="equipment_id" multiple tabindex="10">
                  <option value="" disabled selected>Choose Equipment</option>
                  <option value="1">1</option>
                  <option value="2">2</option>
                  <option value="3">3</option>
                  <option value="4">4</option>
                  <option value="5">5</option>
            </select>
        </div>
        <div class="extras" id="extras"></div>

        <input type="submit" id="submit">
        </form>    
    </body>
</html>

JavaScript (Some of this is thanks to @superUntitled from post Increment multidimensional array when form fields are cloned)

 $(document).ready(function() {   
 $('#repeat').click(function(){
    var newgroup = $('.group:last').clone(true);
    newgroup.find(':input').each(function(){
        this.name = this.name.replace(/\[(\d+)\]/,function(str,p1){return '[' + (parseInt(p1,10)+1) + ']'});


    }).end().appendTo('#extras');
});
 });

This results in an (example) breakdown from the var dump as follows:

array(2) { 
    ["user_id"]=> array(4) { 
        [0]=> string(1) "1" 
        [1]=> string(1) "2" 
        [2]=> string(1) "1" 
        [3]=> string(1) "2" 
    } 
    ["equipment_id"]=> array(4) { 
        [0]=> array(2) { 
            [0]=> string(1) "1" 
            [1]=> string(1) "2" 
        } 
        [1]=> array(3) { 
            [0]=> string(1) "2" 
            [1]=> string(1) "3" 
            [2]=> string(1) "4" 
        } 
        [2]=> array(2) { 
            [0]=> string(1) "4" 
            [1]=> string(1) "5" 
        } 
        [3]=> array(2) { 
            [0]=> string(1) "2" 
            [1]=> string(1) "4" 
        } 
    } 
}

I hope that this is of use to someone in the future!

JS Fiddle if anyone wants it: JSFiddle

Upvotes: 0

Views: 71

Answers (2)

mlambley
mlambley

Reputation: 13

Would something like this help?

<?php
var_dump($_POST);
?>

<form action="#" method="POST">
    <select name="user_id[]">
        <option value="" disabled selected>Choose a User</option>
        <option value="178">178</option>
        <option value="181">181</option>
    </select>
    <select name="equipment_id[178][]" multiple tabindex="10">
        <option value="" disabled selected>Choose Equipment</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
    </select>
    <select name="equipment_id[181][]" multiple tabindex="10">
        <option value="" disabled selected>Choose Equipment</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
    </select>
    <input type="submit" value="Submit" />
</form>

When you select a few items it comes through to the server like:

[ 
    "user_id" => ["178"], 
    "equipment_id" => [
        178 => ["1", "2"],
        181 => ["2", "3"]
    ]
]

So with a few tweaks to your javascript which builds the form, this should do the trick.

Edit: Don't panic's answer is better because it doesn't require the user to be selected before rendering the equipment dropdown.

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

You can name your selects using this pattern:

<select name="users[0][id]">

<select name="users[0][equipment_id]" multiple tabindex="10">

Each time you add a new set of selects with JS, increment the numeric id.

(Like <select name="users[1][id]">)

Then you'll have a $_POST array like this:

['users' => [
    ['id' => 178, 'equipment_id' => [3, 4],
    ['id' => 181, 'equipment_id' => [3, 4, 5],
]

Upvotes: 1

Related Questions