I'm using HTML, JavaScript, Ajax and PHP in this project. I have a form and an onclick event to get the values of the form. The form is being printed out with Ajax and PHP, since it creates the input fields from values stored in a database. I'm trying to get the value from an input field, but it returns empty. If I manually insert the id, the value returns what I want, but now with a variable. I've done this plenty of times but never had this happen, so I'm a bit lost as of the cause of this. Here we have the form:
<p id="arranjos" style="text-align: center;"></p>
<p id="arranjosIDs" value="0" hidden></p>
<button class="btn btn-outline-primary float-right" onclick="save();" style="font-size: 18px; border-radius: 5px;">Save</button>
And here we have the PHP function that creates the form:
function getArranjos($checkedIDs){
global $conn;
$res = "";
$ids = [];
for($i = 0; $i < count($checkedIDs); $i+=2){
$sql = "SELECT * FROM arranjos WHERE idprod = '".$checkedIDs[$i]."' AND estadoArranjo = 'ativo';";
$result = $conn->query($sql);
$res .= "<h2 style='background-color: #007bff; padding: 5px; color: white; border-radius: 5px; margin: 0% 10% 0% 10%;'>".$checkedIDs[$i+1]."</h2><br>";
$res .= "<table style='margin-left: auto; margin-right: auto;'>
<tr>
<th></th>
<th></th>";
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
array_push($ids, $row['idArranjo']);
$res .= "
<tr>
<td style='text-align: right;'>
<label style='font-size: 18px;' for=".$row['idArranjo'].">".$row['descricaoArranjo']." </label>
</td>
<td style='text-align: left;'>
<input style='height: 18;' type='number' value='0' placeholder='0' maxlength='2' size='2' id='".$row['idArranjo']."'>
</td>
</tr>
";
}
$res .= "</table><br><br>";
} else {
$res .= "Não existem arranjos registados para esta peça";
$res .= "</table><br>";
}
}
$resposta = array("res" => $res, "ids" => json_encode($ids));
$resposta = json_encode($resposta);
return($resposta);
}
This prints out OK, I have no problem with it. I have tried changing id='".$row['idArranjo']."' to id=".$row['idArranjo']." since I saw someone with that problem, but it didn't solve it. Here we have the function where it all goes wrong.
console.log(resposta); // [6, 14]
var arranjos = JSON.parse(resposta);
for(y = 0; y < arranjos.length; y++){
console.log(arranjos[y]);
if(arranjos[y] == "none"){
$.ajax({
type: 'POST',
data: {
peça: peçasi,
arranjo: arranjos[y],
op: 10
},
async: false,
url: "paginas/dashboard/model/modelDashboardEntrada.php",
success: function(resposta){
console.log(resposta);
obJSON = JSON.parse(resposta);
$.ajax({
type: 'POST',
data: {
idVenda: obJSON.idVenda,
op: 12
},
async: false,
url: "paginas/dashboard/model/modelDashboardEntrada.php",
success: function(resposta){
console.log(resposta);
}
})
}
})
} else {
var index = String(arranjos[y]); // I have tried this with and without String(), none works
var input0 = document.getElementById(index).value; // If I write "6", it comes out correct, but as it is, just comes out empty. I printed out typeof() and it comes as string
console.log(input0);
}
}