There is a DIV block, which is filled with content after the page is loaded with JS. The task is to save the contents by pressing the button in a file on the server. For example

<div class="getcode"> Наполняемый контент, другие div блоки </div> 

As php means to organize the preservation of internalities, it is a piece of code that is in this block in a file, that is, on the server in a certain daddy put. Html files with the contents of this block.

  • I would do this: by clicking on the button you get element.innerhtml () and send it ajax request to the server, where you save where you want. - koks_rs
  • @koks_rs can be an example of this div'a show?, I'm new to JS and PHP - Pavel

2 answers 2

Html file

 <div class="getcode">Наполняемый контент, другие div блоки</div> <button>Получить и сохранить сожержимое</button> <script> var button = document.querySelector("button"); button.addEventListener("click", sendSave, false); function sendSave() { var data = document.querySelector(".getcode").innerHTML; var formData = new FormData(); formData.append("data", data); var XHR = "onload" in new XMLHttpRequest() ? XMLHttpRequest : XDomainRequest; var xhr = new XHR(); xhr.open('POST', 'save.php', true); xhr.onreadystatechange = () => { if (xhr.readyState !== 4) { return; } if (xhr.status === 200) { console.log(xhr.responseText); } }; xhr.send(formData); } </script> 

PHP file save.php

 <?php $fp = fopen(date('Ymdhis').'-data.html', 'w'); echo (fwrite($fp, $_POST['data'])) ? "Сохранено" : "Не сохранено"; fclose($fp); 
  • When you press the save button, nothing works - Pavel
  • What does he write in the console? A file should appear in the same directory where these 2 files are located. In the console, the text is either saved or not saved. - Nikolai Smekalov
  • if I have html + JS in one file gethtml.php and puy in save.php does not work, and if in one file in save.php then when the page loads it automatically saves, the console issues in 1 and in case 2 save.php : 5 Uncaught TypeError: Cannot read property 'addEventListener' of null (...) (anonymous function) @ save.php: 5 Navigated to dizelist18.ru/lk/admin/save/save.php - Pavel
  • Put js after html - move the script down - Nikolay Smekalov
  • saves, and is it possible to use this technology several times? I mean 3 block divs with different content, but all of the class = getcode will save the content in 1 file - Pavel

First you need to get the content using the POST - GET request:

 <div class="getcode"> <form method="post"> <textarea name="content"> Наполняемый контент, другие div блоки </textarea> <input type="submit" value="Сохранить на html файл" name="otpravit"></input> </form> </div> 

Next, create a file with the received content:

 if (isset($_POST['otpravit'])) { $content = $_POST['content']; } $newFileName = "getcode.html"; $newFileContent = $content; if(file_put_contents($newFileName,$newFileContent)!=false){ }else{ } 
  • Is it possible to do this by pressing a button? - Pavel
  • Added html code there goes on pressing the button of course. - Sauron
  • Now I will correct the answer. So that you can get content with tags. - Sauron