javascriptphphtmlfile-put-contents

Save an html table into a file on server php


I want to save an html table into an excel file, and save it on server with php. I'm sending the html table to php using XMLHttpRequest :

var excel_data = document.getElementById('result-table').innerHTML;

   console.log(excel_data);
   if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          =
        }
    };
     var donn = "filt="+ excel_data;

    xmlhttp.open("POST", "saveToServer.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(donn);

And here's how I retrieve the table fom php side:

$data = $_POST["filt"];
file_put_contents('attachments/excel.xls', $data, FILE_APPEND);

Here's a part of the html table:

<h2 id="searchResultTitle">Today's alarms</h2><table id="dataTable" class="wrap" 
style="position:relative;">
<tbody><tr><th>Raise date</th>
<th>Site</th>
<th>Severity</th>
<th>Alarm</th>
<th>Detail</th>
</tr><tr>
<td style="white-space:nowrap">2020-07-23 14:00:06</td>

<!--Issue happens here-->

<td style="font-size:11px;"><a target="_blank" rel="noopener noreferrer" href="siteDetail.php? 
id=A16X647_HAI EL BADR&amp;fich=huawei-bss-deb-alarm">A16X647_HAI EL BADR</a></td>
<td style="color:red;">Critical</td><td>Commercial Power Down _Main Power Down_</td>
<td><a target="_blank" rel="noopener noreferrer" href="alarmDetail.php?id=90455861&amp;fich=huawei- 
bss-deb-alarm">More</a></td></tr>
...

and here's the excel.xls content:

<h2 id="searchResultTitle">Today's alarms</h2><table id="dataTable" class="wrap" 
style="position:relative;">
<tbody><tr><th>Raise date</th>
<th>Site</th>
<th>Severity</th>
<th>Alarm</th>
<th>Detail</th>
</tr><tr>
<td style="white-space:nowrap">2020-07-23 14:00:06</td>
<td style="font-size:11px;"><a target="_blank" rel="noopener noreferrer" href="siteDetail.php? 
id=A16X647_HAI EL BADR

The main issue that I've been struggling and I don't understand why is why the writing stops at the first <a target="_blank" rel="noopener noreferrer" href="siteDetail.php? id=A16X647_HAI EL BADR

Any help will be very much appreciated.


Solution

  • You are sending the data like a query string but the excel_data variable contains illegal characters (the &) so at your server script the data received is wrong.

    var donn = "filt="+ excel_data;
    
    xmlhttp.open("POST", "saveToServer.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(donn);
    

    The server is receiving:

    filt=<h2 id="searchRe.....HAI EL BADR&amp;fich=huawei-bs.....
    

    Wich is translated as two variables:

    filt = "<h2 id="searchRe.....HAI EL BADR"
    amp;fich = "huawei-bs..."
    

    You need to encode your table string to avoid this.

    var donn = "filt="+ encodeURIComponent(excel_data);