I want to create a table where one of the fields is of the drop-down type, consisting of the key values of an associative array and according to the key that is chosen, a second column is automatically updated with the corresponding value.
Theassociativearrayhasthefollowingform:
$JusRes=array("Jus 1"=>"Resp 1",
"Jus 2"=>"Resp 2",
"Jus 3"=>"Resp 3"
);
javascript function:
<script type='text/javascript'>
function mudaRes(indice){
var just=document.getElementById('Just');
var selectedOption = just.options[just.selectedIndex];
var respValue = selectedOption.getAttribute('Resp');
var resp=document.getElementById('DadosTab');
var Re=document.getElementById('Res');
Re.value = respValue;
resp.rows[indice+1].cells[4].innerHTML = respValue;
}
</script>
This setting only works when I click the first time on the first line, the corresponding data is placed in the Resp column and I can save that data, the problem is when I click on the second line, from here, the column "Resp" nor can I get the data chosen.
I want to save the data, so in order to click "Send" insert in the DB.
Table layout:
<table id='DadosTab'>
<tr>
<th>Nome</th>
<th>Sobrenome</th>
<th>Idade</th>
<th>Just</th>
<th>Resp</th>
</tr>";
echo "<form action='' method='POST'>";
$indtd=0;
$indtr=0;
foreach ($Dados as $chave=>$valor){
echo "<tr id='".$indtr."'>";
//nome
echo "<td align='center'><input type='hidden' name='nome[]' value='".$Dados[$indtr][0]."' />".$Dados[$indtr][0]."</td>";
$indtd++;
//sobrenome
echo "<td align='center'><input type='hidden' name='sobrenome[]' value='".$Dados[$indtr][1]."' />".$Dados[$indtr][1]."</td>";
$indtd++;
//idade
echo "<td align='center'><input type='text' name='idade[]' value='".$Dados[$indtr][2]."' /></td>";
$indtd++;
//echo "<td align='center'>".$Dados[$ind][2]."</td>";
echo "<td><select id='Just' name='Just[]' style='width: 220px' onchange=mudaRes($indtr)>";
$indtd++;
echo "<OPTION>Tipo de Justificacao</OPTION>";
foreach ($JusRes as $Jus=>$Resp){
echo "<OPTION id='".$indtr."' Resp='".$Resp."' value='".$Jus."'>".$Jus."</OPTION>";
}
echo "</select></td>";
echo "<td align='center'><input id='Res' type='hidden' name='Res[]' /></td>";
$indtd++;
echo "</tr>";
$indtr++;
}
echo "<tr><th align='left' colspan='5'><input type='submit' value='Send' name='Send'></th></tr>";
echo "</form>";
Can you help me?