-1
How to pass the Array newArray
from Javascript to a PHP vector? The idea is to take the value id
of each checkbox and put in a vector in PHP so that these id’s are treated in a database (as primary key). That code already takes checkbox id’s marked and shows them in a window.
Example:
Idcheckbox: 1, 2, 3
Vetorphp: [1,2,3]
function coletaDados() {
var ids = document.getElementsByClassName('editar');
coletaIDs(ids);
}
function coletaIDs(dados) {
var array_dados = dados;
var newArray = [];
for (var x = 0; x <= array_dados.length; x++) {
if (typeof array_dados[x] == 'object') {
if (array_dados[x].checked) {
newArray.push(array_dados[x].id)
}
}
}
if (newArray.length <= 0) {
alert("Selecione um pelo menos 1 item!");
} else {
alert("Seu novo array de IDs tem os seguites ids [ " + newArray + " ]");
}
}
<table border="1">
<thead>
<tr>
<th>Id</th>
<th>Nome</th>
<th>Categoria</th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="editar" type="checkbox" id="01" value="Barco"></td>
<td>Barco</td>
<td>Não definido</td>
</tr>
<tr>
<td><input class="editar" type="checkbox" id="02" value="Carro"></td>
<td>Carro</td>
<td>não definido</td>
</tr>
<tr>
<td colspan="3">
<button style="width:100%;" onclick="coletaDados()">Editar</button>
</td>
</tr>
</tbody>
</table>
Besides looking duplicated, your question is a little vague. You just showed the code, but did not give many details of what you intend to do.
– Wallace Maxters
@Wallacemaxters, the question has been edited. It’s clearer now?
– Marvin
Now it’s filet..
– Wallace Maxters