The page index.php make an AJAX call to execute this code in sauver_page.ajax.php:
<?php
header('Content-Type: application/json');
$retour = array();
$file = $_SERVER['DOCUMENT_ROOT'].'/w/test_save/cible.php';
$htmlFile = new DOMDocument('1.0',"UTF-8");
$htmlFile->loadHTMLFile($file);
$contentEditable = $htmlFile->getElementById("editable");
if($contentEditable) {
$retour["contentEditableValue"] = $contentEditable->nodeValue;
$fragment = $contentEditable->ownerDocument->createDocumentFragment();
$fragment->appendXML("<P>voilà un nouveau doc ".date('Y-m-d H:i:s')."</P>");
$clone = $contentEditable->cloneNode(); // Get element copy without children
$clone->appendChild($fragment);
$retour["cloneId"] = print_r($clone->getAttribute('id'), true);
$retour["cloneValue"] = print_r($clone->nodeValue, true);
$newnode = $htmlFile->importNode($clone, true);
$htmlFile->appendChild($newnode);
$retour["replaceSuccessRetour"] = $contentEditable->parentNode->replaceChild($newnode, $contentEditable);
//$retour["replaceSuccessRetour"] = $contentEditable->parentNode->replaceChild($contentEditable, $newnode);
if(!$retour["replaceSuccessRetour"]){
$retour["replaceSuccess"] = "échouée";
} else {
$retour["replaceSuccess"] = "ok";
}
//$contentEditable = $htmlFile->getElementById("editable");
$retour["contentEditableModif"] = $contentEditable->nodeValue;
$retour["sauve"] = $htmlFile->saveHTMLFile($file);
if(!$retour["sauve"]){
$retour["sauvegarde"] = "échouée";
} else {
$retour["sauvegarde"] = "ok";
}
} else {
$retour["contentEditable"] = "pas de balise 'editable'";
}
echo json_encode($retour);
?>
And i am surprised to observe that in response of the AJAX call :
contentEditableValue "ouuuups"
cloneId "editable"
cloneValue "voilà un nouveau doc 2021-10-08 09:37:46"
replaceSuccessRetour Object { }
replaceSuccess "ok"
contentEditableModif "ouuuups"
sauve 220
sauvegarde "ok"
contentEditableModif
"ouuuups" must be the new one value like voilà un nouveau doc 2021-10-08 09:37:46
!
And the $htmlFile->saveHTMLFile($file);
works, it contains the good new value : voilà un nouveau doc 2021-10-08 09:37:46
Question : Why $retour["contentEditableModif"]
stay with the old value ?
=== Other interessant subject :