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 theequipment_id[]field so that I can extract as sub-array usinguser_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
?user_id[178][]=3&user_id[178][]=4