PHP - loadHTMLFile () does not load the html document

1

I am trying to modify the code of an html page after filling out a form with the new data that has to be displayed. When I save the form I am directed to a php file to save the changes in the database and from there access the .html file to modify it:

<?php

// Guardar cambios en base de datos OK

//Modificar html
$error="";
$file = "cIndex.html"; // el fichero se encuentra en la misma ruta 
$DOM = new DOMDocument();
if( file_exists("cIndex.html") == true )
   $DOM->loadHTMLFile($file);   //entra aqui, fichero encontrado
else
   $error = "No existe";  

$DOM->loadHTMLFile($file);

$cName = $DOM->getElementsById('cName'); //al añadir esta línea me da error 500 Internal Server Error. Si la quito $DOM lo devuelve vacío.

$DOM->saveHTML(); 

$respuesta = array (
    "error" => $error, //""
    "file" => $file, // "cIndex.html"
    "DOM" => $DOM  //lo devuelve vacío {}
);
echo json_encode($respuesta);
    
asked by Maria Jo 11.05.2017 в 12:01
source

1 answer

0

The call to DOMDocument :: saveHTML () returns a string containing the HTML content generated by the DOM.

The correct way to use it would be:

<?php
// Guardar cambios en base de datos OK
//Modificar html
$error = "";
$file = "cIndex.html"; // el fichero se encuentra en la misma ruta 
$DOM = new DOMDocument();
if( file_exists($file) == true ) {
   $DOM->loadHTMLFile($file);   //entra aqui, fichero encontrado
} else {
   $error = "No existe";  
}

$DOM->loadHTMLFile($file);

/* Aquí puedes hacer lo que quieras con el DOM */
$cName = $DOM->getElementById('cName');

$html = $DOM->saveHTML(); 

$respuesta = array (
    "error" => $error, //""
    "file" => $file, // "cIndex.html"
    "DOM" => $html  //lo devuelve vacío {}
);
echo json_encode($respuesta);

I've changed DOMDocument::getElementsById() to DOMDocument::getElementById() .

    
answered by 11.05.2017 / 12:37
source