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?
$_POST['firstname']is a PHP variable, and it is determined exactly one time, when your PHP code runs on the server. You can't change it based on something the client does. In other words, youronclickactions will always have the same value on any given page load; the user can't change the PHP variables because they aren't variables anymore; they're values. If the client submits a form, of course, then whatever they submit is available to whatever PHP code handles that form submission.