So, I'm using PDO to store data from a database. I have an array ($myitems) with a few items where each item has two attributes: id and name. with PHP I created a list using this array, like you can see in the code below:
in index.php
<ul class="items">
<?php foreach($myitems as $item): ?>
<li class="test">
<form action="edit.php" method="post">
<span class="itemlist"><?php echo $item['name']; ?></span>
</form>
</li>
<?php endforeach; ?>
</ul>
So when the user double clicks in an item of the list, it "transforms" the span into an input text, so the user can update de name of the item. I managed to do this using JQuery where I'm able to get the name of the item because it's being displayed in the span tag. This is how I'm getting the name's value and changing the the span to an input text:
in JS file
$(".itemlist").dblclick(function (e) {
e.stopPropagation();
var currentEle = $(this);
var name = $(this).html();
updateVal(currentEle, name);
});
function updateVal(currentEle, name) {
$(document).off('click');
$(currentEle).html('<input class="liVal" name="editname" type="text" value="' + name+ '" />');
$(".thVal").focus();
$(".thVal").keyup(function (event) {
if (event.keyCode == 13) {
$(currentEle).html($(".liVal").val());
}
});
}
It works fine in browser but to update the value of the item in the database I need to submit the form, and this is how I'm doing it:
also in JS file
$(".liVal").onkeyup(function(event) {
if (event.keyCode == 13) {
this.form.submit();
return false;
}
});
After submit the form, I can get the value of the item's name in the edit.php using the input's attribute name, like this:
in the edit.php
if(isset($_POST['editname'])){
$newname= trim($_POST['editname']);
}
But to update an item I also need its id. And here is THE PROBLEM, how can I get the id? I really new in PHP and JQuery, I need help with this.