I'm starting to learn javascript now, and I have to fill out a form with some data I have in a bank. When doing the query in the database (through php), u have a multidimensional array like this:
Array ( [0] => Array ( [evento_id] => 2 [celebracao] => 3 [eventoNome] => ANIVERSÁRIO ARTHUR 2 ANOS [primeiroNome] => ARTHUR HENRIQUE [segundoNome] => [data] => 2018-04-08 [observacoes] => ) [1] => Array ( [evento_id] => 3 [celebracao] => 3 [eventoNome] => ANIVERSÁRIO DO ANDERSON [primeiroNome] => ANDERSON MOREIRA [segundoNome] => [data] => 2017-10-21 [observacoes] => ) )
The form is made from a select, as in the code below:
<div class="form-input-box">Evento: <select id="evento" name="evento" onchange="atualiza(this.value)">
<?php foreach ($evento as $row) {
echo "<option value=".$row['evento_id'].">".$row['eventoNome']." </option>";}?>
</select></div>
<div class="form-input-box">Data: <input type="date" data-format="dd-mm-yyyy" name="data" id="data" disabled/></div>
<?php
if ($evento == null) {
echo "<div class='form-input-box'>Noivo: <input type='text' name='primeiroNome' id='primeiroNome' disabled/></div>";
echo "<div class='form-input-box'>Noiva: <input type='text' name='segundoNome' id='segundoNome' disabled/></div>";
} else {
echo "<div class='form-input-box'>Nome: <input type='text' name='primeiroNome' id='primeiroNome' disabled/></div>";}
I know that I need the ids inside the inputs to receive the javascript data, but I'm not sure how to get the array that is in php, the value of the select where it will already be with the name of the chosen event (ANNIVERSARY ARTHUR 2 YEARS value 2 or ANDERSON ANNIVERSARY value 3) and update the name and date fields of the form below the select. The script I made for this is this:
function atualiza(valor) {
var eventoid = valor;
var eventos = <?php echo json_encode($evento);?>;
for(i=0; i<= eventos.length; i++){
for (j=0; j<=eventos.length; j++){
if (eventos.evento_id = eventoid){
document.getElementById('primeiroNome').innerHTML=(eventos.primeiroNome);
document.getElementById('segundoNome').value=(eventos.segundoNome);
document.getElementById('data').value=(eventos.data);
document.getElementById('evento_id').value=(eventos.evento_id);
}
}
}
}
But this update is not happening, what can I be doing. Any help is welcome.