Javascript 将html表保存到php服务器上的文件中

Javascript 将html表保存到php服务器上的文件中,javascript,php,html,file-put-contents,Javascript,Php,Html,File Put Contents,我想将一个html表格保存到一个excel文件中,并用php将其保存在服务器上。我正在使用XMLHttpRequest将html表发送到php: var excel_data = document.getElementById('result-table').innerHTML; console.log(excel_data); if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera

我想将一个html表格保存到一个excel文件中,并用php将其保存在服务器上。我正在使用XMLHttpRequest将html表发送到php:

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);
下面是我如何检索php端的表:

$data = $_POST["filt"];
file_put_contents('attachments/excel.xls', $data, FILE_APPEND);
以下是html表的一部分:

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

我一直在努力解决的一个主要问题是,我不明白为什么在第一次写入时就停止了
您正在像查询字符串一样发送数据,但是
excel\u data
变量包含非法字符(
&
),因此在服务器脚本中接收到的数据是错误的

var donn = "filt="+ excel_data;

xmlhttp.open("POST", "saveToServer.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(donn);
服务器正在接收:

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

最后一个
我只是粘贴了表的一部分,因为内容有点长。
filt=<h2 id="searchRe.....HAI EL BADR&amp;fich=huawei-bs.....
filt = "<h2 id="searchRe.....HAI EL BADR"
amp;fich = "huawei-bs..."
var donn = "filt="+ encodeURIComponent(excel_data);