0

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:

  • Each time I want to add another group of "user_id[]" and "equipment_id[] fields, I use a javascript function to duplicate them.
  • In the "user_id[]" field, I can select one user at a time.
  • In the "equipment_id[]" field, I can select multiple options from a list of equipment.

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:

  • Is there a way of using the user_id[] field as a key for the equipment_id[] field so that I can extract as sub-array using user_id[]?

  • If not, is there another way of cataloguing the equipment_id[] field selections by user and extract them?

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

2
  • 1
    You can have several equipment selects which are shown / hidden when a user is selected. Otherwise you would have to use Javascript to build your assignments and submit this with AJAX. Commented Nov 20, 2019 at 22:58
  • This is possible in PHP, however, I believe javascript is going to be required to make the request. You are going to send something like ?user_id[178][]=3&user_id[178][]=4 Commented Nov 20, 2019 at 23:10

2 Answers 2

1

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],
]
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Don't Panic; I have implemented this, the result I am getting is: ` array(1) { ["work"]=> array(1) { [0]=> array(4) { ["user_id"]=> string(3) "181" [0]=> array(1) { ["equipment_id"]=> string(1) "3" } [1]=> array(1) { ["equipment_id"]=> string(1) "4" } [2]=> array(1) { ["equipment_id"]=> string(1) "3" } } } } ` Its including an equipment item from another user_id selection :0)
Hm, seems on the right track but not quite there. Do you think you could send me a link to a fiddle with the form code? Maybe I can see what the problem is
Hi @Don't Panic; thank you for your feedback so far! I've put the code into a jsfiddle at [ jsfiddle.net/1f5rydk7 ] but I executed it on [ phpfiddle.org ]
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.