I'm trying to create an editable line, so when I click on it, Js brings me the inputs, allowing me to make the changes on that line and perform the Update in the database.
As in this image, that prinlt of Trello.
Note that it's a line, but when I click on it, the application brings me a field to edit and save.
functioneditarPedido(){varalterar=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 on the first line. I can not get the inputs to appear on the line the click was given and to change it.
So I had the idea to send somehow, the ID for this function in Javascript, or better, send it to getElementById
. But nothing I've tried so far has worked on the mission of trying to redeem the ID in a JS function.
In this case, is my methodology correct to do this kind of thing? Or does it have security holes? Are there better ways to do this?