繁体   English   中英

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

[英]Save an html table into a file on server 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>
...

这是excel.xls内容:

<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

我一直在努力的主要问题,我不明白为什么写作停止在第一个<a target="_blank" rel="noopener noreferrer" href="siteDetail.php? id=A16X647_HAI EL BADR

任何帮助将不胜感激。

您正在像查询字符串一样发送数据,但excel_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.....

Wich 被翻译为两个变量:

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

您需要对表字符串进行编码以避免这种情况。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM