1
Example scenario
I have a form
which it possesses in the name, 2 specific values: X[Y]
X is a reference to the vision code, and Y concerning the field of my vision, and the value of input
, shall be the value for XY.
Could it be considered a matrix!? Anyway...
The example is below:
function clone($e) {
$c = $($e).parents("tr");
$($c).clone().appendTo("#clones");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="FormABC" onsubmit="return false">
<table>
<thead>
<tr>
<th>Fixo 1</th>
<th>Fixo 2</th>
<th>Fixo 3</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="date" name="fx[a]" value="2020-01-01"></td>
<td><input type="text" name="fx[b]" value="teste"></td>
<td><input type="number" name="fx[c]" value="123"></td>
</tr>
</tbody>
</table>
<br><br>
<table>
<thead>
<tr>
<th></th>
<th class="table-secondary">Campo 1</th>
<th class="table-secondary">Campo 2</th>
<th class="table-secondary">Campo 3</th>
</tr>
</thead>
<tbody>
<tr>
<th class="table-secondary"><button onclick="clone(this)">Visão 1</button></th>
<td><input type="number" name="1[1]" value="1"></td>
<td><input type="number" name="1[2]" value="2"></td>
<td><input type="number" name="1[3]" value="3"></td>
</tr>
<tr>
<th class="table-secondary"><button onclick="clone(this)">Visão 2</button></th>
<td><input type="number" name="2[1]" value="4"></td>
<td><input type="number" name="2[2]" value="5"></td>
<td><input type="number" name="2[3]" value="6"></td>
</tr>
</tbody>
</table>
</form>
<br><br>
<div id="clones"><p>Clones</p></div>
The problem
Above, when I make a "clone", it will bring me a copy, but when I use the serialize()
, it will override the values for sending, and so will only the last values according to sequence html.
Example: If I clone vision 2 and replace the values for 7, 8 and 9, my return will be:
Array
(
[fx] => Array
(
[a] => 2020-01-01
[b] => teste
[c] => 123
)
[1] => Array
(
[1] => 1
[2] => 2
[3] => 3
)
[2] => Array
(
[1] => 7
[2] => 8
[3] => 9
)
)
Example of the possible solution
What I thought, would add an "accountant" at the beginning of name.
For example:
ANTES:
<tr>
<th class="table-secondary"><button onclick="clone(this)">Visão 1</button></th>
<td><input type="number" name="1[1]" value="1"></td>
<td><input type="number" name="1[2]" value="2"></td>
<td><input type="number" name="1[3]" value="3"></td>
</tr>
DEPOIS:
<tr>
<th class="table-secondary"><button onclick="clone(this)">Visão 1</button></th>
<td><input type="number" name="1[1][1]" value="1"></td>
<td><input type="number" name="2[1][2]" value="2"></td>
<td><input type="number" name="3[1][3]" value="3"></td>
</tr>
That way, I would have each individual value, keeping my X and Y, adding the "counter" in front C[X][Y].
Exit:
Array
(
[fx] => Array
(
[a] => 2020-01-01
[b] => teste
[c] => 123
)
[1] => Array
(
[1] => Array
(
[1] => 1
)
)
[2] => Array
(
[1] => Array
(
[2] => 2
)
)
[3] => Array
(
[1] => Array
(
[3] => 3
)
)
[4] => Array
(
[2] => Array
(
[1] => 4
)
)
[5] => Array
(
[2] => Array
(
[2] => 5
)
)
[6] => Array
(
[2] => Array
(
[3] => 6
)
)
[7] => Array
(
[2] => Array
(
[1] => 7
)
)
[8] => Array
(
[2] => Array
(
[2] => 8
)
)
[9] => Array
(
[2] => Array
(
[3] => 9
)
)
)
The difficulty would be to catch this name, treat one by one not to define a value already used in another element, and thus override that value.
Doubt
- I wonder how I could solve this problem?
Do you want to serialize inputs out of the form? Serialize only works with form.
– Sam
No, everything inside. In the code is inside the form... sorry... kkk
– rbz
I thought of a way, using the id, but it is a braba gambiarra... I prefer to see other more correct alternatives...
– rbz
Then @Rbz. Take a look at the answer and tell me what you found. Dude, I spent almost 2 hours on this between understanding what you wanted and coming up with a solution. But I think it looks good. I hope it works out for you. =]
– Sam