我想將html表保存到excel文件中,并使用php將其保存在服務(wù)器上。我使用 XMLHttpRequest 將 html 表發(fā)送到 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&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&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貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個贊
您正在像查詢字符串一樣發(fā)送數(shù)據(jù),但excel_data
變量包含非法字符( )&
,因此在您的服務(wù)器腳本中收到的數(shù)據(jù)是錯誤的。
var donn = "filt="+ excel_data; xmlhttp.open("POST", "saveToServer.php", true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.send(donn);
服務(wù)器正在接收:
filt=<h2 id="searchRe.....HAI EL BADR&fich=huawei-bs.....
這被翻譯為兩個變量:
filt = "<h2 id="searchRe.....HAI EL BADR" amp;fich = "huawei-bs..."
您需要對表字符串進(jìn)行編碼以避免這種情況。
var donn = "filt="+ encodeURIComponent(excel_data);
- 1 回答
- 0 關(guān)注
- 144 瀏覽
添加回答
舉報
0/150
提交
取消