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

我想将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>

...


我一直在努力并且不明白为什么的主要问题是为什么写作一开始就停止了<a target="_blank" rel="noopener noreferrer" href="siteDetail.php? id=A16X647_HAI EL BADR



暮色呼如
浏览 82回答 1
1回答

aluckdog

您正在像查询字符串一样发送数据,但excel_data变量包含非法字符( )&,因此在您的服务器脚本中收到的数据是错误的。var&nbsp;donn&nbsp;=&nbsp;"filt="+&nbsp;excel_data; xmlhttp.open("POST",&nbsp;"saveToServer.php",&nbsp;true); xmlhttp.setRequestHeader("Content-type",&nbsp;"application/x-www-form-urlencoded"); xmlhttp.send(donn);服务器正在接收:filt=<h2&nbsp;id="searchRe.....HAI&nbsp;EL&nbsp;BADR&amp;fich=huawei-bs.....这被翻译为两个变量:filt&nbsp;=&nbsp;"<h2&nbsp;id="searchRe.....HAI&nbsp;EL&nbsp;BADR" amp;fich&nbsp;=&nbsp;"huawei-bs..."您需要对表字符串进行编码以避免这种情况。var&nbsp;donn&nbsp;=&nbsp;"filt="+&nbsp;encodeURIComponent(excel_data);
打开App,查看更多内容
随时随地看视频慕课网APP