What I'm trying to achieve is to display the inputted name in text.The user is allowed to edit it by clicking the edit button. A form will replace the text and the form must have the default value of the name.
My code is able to perform the process stated. The problem is when I click the save button, the displayed text is still the old value. It doesn't update.
function edit(firstname){
document.getElementById("firstname-div").innerHTML='First Name:<input type="text" name="firstname" value="'+firstname+'">';
}
function save(firstname){
document.getElementById("firstname-div").innerHTML= 'First Name:'+firstname;
}
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<button type="button" onclick="edit('<?php echo $firstname ?>')">edit</button><br>
<button type="button" onclick="save('<?php echo $_POST['firstname']?>')">save</button><br>
I know it has something to do with <button type="button" onclick="save('<?php echo $_POST['firstname']?>')">save</button>
. The $_POST['firstname']
value is still the same.
Any idea on how to solve this? Or other implementation in pure javascript?