Save content from a div to a .html file With php or javascript

-1

How do I save content from a div into a .html file with php or javascript.

The intention is to generate a pdf later with this saved html. I saw that there are many topics here on the subject, but I'm failing miserably, none of them helped me, because I need to save this html before.

Thank you

    
asked by anonymous 17.06.2018 / 22:54

2 answers

2

Javascript

Take the contents of <div class="publicar"> and play within <textarea id="qqID"... to be sent via post to PHP

function CopiaDivParaTextArea()
{
   var divs = document.getElementsByTagName("div");
   var conteudoParaPublicar = "";
   var pattern = new RegExp("publicar");

   for(var i=0;i<divs.length;i++){
     if(pattern.test(divs[i].className)){
       conteudoParaPublicar += (divs[i].innerHTML || divs[i].textContent);
     }
   }
 document.getElementById("qqID").value = conteudoParaPublicar;
 document.formulario.submit();
}

HTML

<div class="publicar"> <p>Este conteudo será salvo em uma pagina no servidor</p></div>
<div class=""> <p>Este conteudo NÃO será salvo pois a class não é "publicar"</p></div>
<div class="publicar"> <p>Este tambem será publicado na página</p></div>

<form name="formulario" action="" method="post">
    <textarea id="qqID" name="qqnome" style="display:none"></textarea>
    <button type="button" id="btn" value="Button" onclick="CopiaDivParaTextArea();" />Publicar</button>
</form>

PHP

if (isset($_POST['qqnome'])){

    $conteudo = $_POST['qqnome'];
    //gera um numero aleatorio para concatenar com o nome do arquivo
    $num = mt_rand();

    //echo "nome do arquivo publicado: pagina".$num.".html";

    //salva o arquivo no servidor
    file_put_contents("pagina".$num.".html", $conteudo);

}
  

Following is a test of the part where the div is passed to textarea

    function CopiaDivParaTextArea()
    {
       var divs = document.getElementsByTagName("div");
       var conteudoParaPublicar = "";
       var pattern = new RegExp("publicar");

       for(var i=0;i<divs.length;i++){
         if(pattern.test(divs[i].className)){
           conteudoParaPublicar += (divs[i].innerHTML || divs[i].textContent);
         }
       }
     document.getElementById("qqID").value = conteudoParaPublicar;
    }
    <button type="button" id="btn" value="Button" onclick="CopiaDivParaTextArea();" />Testar</button>
	<div class="publicar"> <span>Este conteudo será salvo em uma pagina no servidor</span></div>
	<div class="">Este conteudo NÃO será salvo pois a class não é "publicar"</div>
	<div class="publicar"><span style="color:red">Este também será publicado na página</span></div>

	<textarea id="qqID" name="qqnome" rows="4" cols="47"></textarea>
	
    
18.06.2018 / 01:21
1

There are a number of ways, one of which is fwrite native php:

$fp = fopen('data.html', 'w');
fwrite($fp, '<div>texto');
fwrite($fp, '<br>mais texto</div>');
fclose($fp);

The content of data.html is now <div>texto<br>mais texto</div>

If the server is on a linux you can use the exec function to execute something like:

exec("echo '<div>texto' >> data.html");
exec("echo '<br>mais texto</div>' >> data.html");
// Ou
exec("echo '<div>texto<br>mais texto</div>' > data.html");

The difference between > and >> , is that the first just overwrites what's inside the file, the second, adds with a line break

    
17.06.2018 / 23:43