There is a code:

<?php $fp = fopen(date('Ymdhis').'-data.html', 'w'); $html = ""; foreach($_POST[data] as $data) { $html .= $data.PHP_EOL; } echo (fwrite($fp, $html)) ? "Сохранено" : "Не сохранено"; fclose($fp); ?> 

Js + html

 <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> 

The contents of the div block is saved to a file on the server at the click of a button. How to implement it so that when saving on the page, even through input, transfer the name of the file that is saved.

    1 answer 1

    FormData third parameter, just the same, the name of the file that will be sent to the server (USVString) when Blob or File passed the test as the second parameter. The default filename for Blob objects is "blob".

    Example:

     var formData = new FormData(); // Currently empty formData.append('username', 'Chris'); formData.append('userpic', myFileInput.files[0], 'chris.jpg'); 

    reference

    Accordingly, the values ​​of the input are passed by the third parameter and it seems like this is all. On the server, respectively, get out.


    For example, input <input type="text" id="test" value="myTextValue"> , we get this way:

     var txtName = document.querySelector('#test').value; 

    We pass:

     var txtName = document.querySelector('YOUR_SELECTOR').value; var formData = new FormData(); // Currently empty formData.append('username', 'Chris'); formData.append('userfile', myFileInput.files[0], txtName); 

    The truth is as far as files are concerned ..

    If you pass a string in the value, then from the input you also need to add a new passed parameter with your key, but with the value from the input, i.e.

     var data = document.querySelector(".getcode").innerHTML; var fileName = document.querySelector('YOUR_SELECTOR').value; var formData = new FormData(); // Currently empty formData.append('data ', data); formData.append('fileName', fileName); 

    On the server, catch the variable from $_POST['fileName']


    Total

    HTML

     <div class="getcode">Наполняемый контент, другие div блоки</div> <button>Получить и сохранить сожержимое</button> <input type="text" id="fileName" value="myTextValue"> 

    Js

     var button = document.querySelector("button"); button.addEventListener("click", sendSave, false); function sendSave() { var data = document.querySelector(".getcode").innerHTML; var fileName = document.querySelector('#fileName').value; var formData = new FormData(); formData.append('data', data); formData.append('fileName', fileName); 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); } 

    Php

     $fp = fopen($_POST['fileName'], 'w'); // $fp = fopen($_POST['fileName'].'-data.html', 'w'); $html = $_POST['data'].PHP_EOL; echo (fwrite($fp, $html)) ? "Сохранено" : "Не сохранено"; fclose($fp); 
    • can you help with modifying my code ?, I don’t quite understand what and how to use, how can I connect it? if you register it? - Pavel
    • @ Paul wrote. but it's kind of standard - Alexey Shimansky
    • does not work because form data is another variable in general. The name is created by this line $ fp = fopen (date ('Ymdhis') .'- data.html', 'w'); . Here’s how to put <input type = "text" id = "test" value = "myTextValue"> into this line, - Pavel
    • @Pavel mmm because you pass a string, not a file, then it suffices to add a new transmitted parameter with your key, but with a value from the input (see at the bottom of the answer) - Alexey Shimansky
    • does not work, does not work. pereolopil all code. Help in the code for the question please build in ( - Pavel