How to pass Array newArray
of Javascript to a vector in PHP? The idea is to get the id
value of each checkbox and put it in a vector in PHP so that these id's are treated in a database (as primary key). This code already picks the id's from checked checkbox's and shows them in a window.
Example:
IDcheckbox: 1, 2, 3
VectorPHP: [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>