为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
暮色呼如 2023-08-19 16:54:52
我想将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
查看完整描述

1 回答

?
aluckdog

TA贡献1847条经验 获得超7个赞

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

这被翻译为两个变量:

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

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

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


查看完整回答
反对 回复 2023-08-19
  • 1 回答
  • 0 关注
  • 79 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信