1
I’m trying to create an editable line, so that when I click on it, Js brings me the inputs, allowing me to make the changes in that line and perform the Update in the database.
Like this picture I printed of Trello.
Note that it is a line, but when I click on it, the application brings me a field to edit and save.
function editarPedido() {
var alterar = document.getElementById("editar");
alterar.innerHTML =
"<input type='text' name='alt-nome'>"+
"<input type='text' name='alt-causa'>"+
"<input type='submit' name='salvar' value='Salvar'>"+
"</form>";
}
<ul>
<?php if(!empty($campos)):
foreach($campos as $campo): ?>
<li class='lista-pedidos'>Nome: <?=$campo->nome?> - Causa: <?=$campo->causa?>
<a href="#" onclick="editarPedido()"> Editar</a>
<form action='action.php' method='post'>
<input type='hidden' name='id' value='<?=$campo->id?>'>
<div id="editar"></div>
</form>
</li>
<?php endforeach;
endif; ?>
</ul>
This even works, but when I click edit, the inputs always appear in the first line. I can’t make, by clicking on any line, the inputs appear on the line the click was given and allow it to be changed.
So I had the idea of sending somehow, the ID to this function in Javascript, or better, send to the getElementById
. But nothing I’ve tried so far has worked, in trying to rescue the ID in a JS function.
In this case, is my methodology correct to do this kind of thing? Or does it have security flaws? There are better ways to do this?