2
I am recording data from 6 checkboxes in the database. I am treating each one separately, as below:
<tr>
<td>Tabela Nutricional:</td>
<td><input type="checkbox" name="export_tabela[1]" id="export_tabela[1]" value="PORTUGUES"> Português
<input type="checkbox" name="export_tabela[2]" id="export_tabela[2]" value="INGLES"> Inglês
<input type="checkbox" name="export_tabela[3]" id="export_tabela[3]" value="ESPANHOL"> Espanhol
<input type="checkbox" name="export_tabela[4]" id="export_tabela[4]" value="FRANCES"> Francês
<input type="checkbox" name="export_tabela[5]" id="export_tabela[5]" value="ARABE"> Árabe
<input type="checkbox" name="export_tabela[6]" id="export_tabela[6]" value="COREANO"> Coreano</td>
</tr>
I can record all that were marked in the registration page and in the table is correctly:
My problem is that when it comes to rescuing the data using Ajax, it does not mark any value in the checkbox of the editing page, whose HTML is the same as the registration page. The Ajax I use, shown below, saves correctly in all other fields, takes the id and saves the value:
// Verifica se foi concluído com sucesso e a conexão fechada (readyState=4)
if (xmlreq.readyState == 4) {
// Verifica se o arquivo foi encontrado com sucesso
if (xmlreq.status == 200) {
//Se o retorno foi vazio do MySQL
if (xmlreq.responseText == "") {
document.getElementById("projeto").focus();
alert("Não existe o projeto informado!");
ids.forEach(function (id) {
document.getElementById(id).value = '';
document.getElementById("projeto").value = '';
});
//Se encontrou dados
} else {
//Aqui recebe os dados do processa.php, abre e aplica nos campos desejados
var dados = JSON.parse(xmlreq.responseText);
// função para preencher os campos com os dados
ids.forEach(function (id) {
document.getElementById(id).value = dados[id];
});
}
} else {
result.innerHTML = "Erro: " + xmlreq.statusText;
}
}
Any suggestions how I can bring the fields and mark them on the update page?
I think at the same time
.value
you want to use.checked
which is the property that marks/unchecks. How does this database value come and then JSON parse? comes as string or Boolean? If it’s boolean just use:document.getElementById(id).checked = dados[id];
– Sergio
The data goes to ajax as array. Vc says edit ajax at the time of "load" of the values?
– Diego
From what I read in the question, the send-to-BD part works, but you’re having trouble marking/unchecking when you want to reassemble that information with BD data, right? in this case depends on how Ravas in the BD and how you return via ajax. If you save the status
checked
/not checked
as string or as Boolean?– Sergio
Exactly, the recording is ok, my problem is in return. Actually I did not save the state, saved the field value: <input type="checkbox" name="export_table[1]" id="export_table[1]" value="PORTUGUES">. How I record the state?
– Diego
It depends on how you send it to the server. If you send a form to PHP only the ones that are marked are passed with the form. If you go through ajax you have to check the
input.checked
and delete the ones that aren’t marked, if it’s the first case then you can just do.checked = true;
for those in the database, that is in the JSON that came from the server.– Sergio
Thanks for the help @Sergio, I used your suggestion from . checked, I divided the way to rescue the data and it worked. Thanks.
– Diego
You’re welcome. I’ve put together a more complete answer and a suggestion for optimisation.
– Sergio