0

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 :

Naeco
  • 133
  • 1
  • 1
  • 9
  • 1
    Just because you called `$contentEditable->parentNode->replaceChild($newnode, $contentEditable)`, does not mean what the variable `$contentEditable` contains, has also changed. That is still referring to your _old_ node. – CBroe Oct 08 '21 at 09:55
  • Okay, but in this case, why `$htmlFile->saveHTMLFile($file)` save the good things ? I have tried with uncomment the line `//$contentEditable = $htmlFile->getElementById("editable");`, and it is the same result. – Naeco Oct 08 '21 at 11:35
  • 1
    Because `$htmlFile` refers to your whole DOMDocument object, and in that you replaced the existing node with a different one. – CBroe Oct 08 '21 at 11:44

0 Answers0