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

Asked

Viewed 693 times

-1

How do I save content from a div to a file. html 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 am failing miserably, none of them helped me because I need to save this html before.

Thank you

  • 1

    Save as? Take the contents of div and save where? on the server as HTML page?

  • This, simply save to any . html file inside the same server.

  • 1

    Another thing, this div is on another page?

  • It’s on the same page.

2 answers

2


Javascript

takes the contents of <div class="publicar"> and plays within the <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);

}

The following is a test of the part where the div is passed to the 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>
	

  • Are you familiar with a gaga table? Check it here: https://answall.com/q/308044/8063

1

There are several forms, one of them is with the function fwrite php native:

$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 function exec 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 only overwrites what is inside the file, the second, adds with a line break

Browser other questions tagged

You are not signed in. Login or sign up in order to post.