下面的代码在一台服务器上按预期工作,但在另一台服务器上失败。预期的行为是在添加一些过滤器值并应用过滤器后,过滤器值应该保留在那里。
<!DOCTYPE html>
<?php
// Initialisation
$numbersPerPage = array( "10", "25", "50", "100" );
$filterOptions = array( "",
"one",
"two",
"three",
);
?>
<html lang="en">
<head>
<style>thead {color:black;font-weight: bold}
tbody {color:black;}
tfoot {color:red;}
table,th,td
table {table-layout:fixed;word-wrap:break-word;}
</style>
<meta charset="utf-8">
<title>Track & Trace</title>
<meta name="description" content="Track and trace main page">
<meta name="author" content="Marc Fellman">
<meta name="google" value="notranslate">
<script language="javascript">
<!--
//Browser Support Code
function filterOption(action,loc){
num = parseInt(loc.split("-")[1]) + 1;
tableNode = document.getElementById("Filters");
if ( action.trim() == "add" ) {
newRow = tableNode.insertRow(tableNode.rows.length - 1);
newRow.id = "Filter-" + newRow.rowIndex;
newCell = newRow.insertCell(0);
newRemoveButton = document.createElement("span");
newRemoveButton.innerHTML = '<button type="button" onclick="filterOption(\'remove\',\'Filter-' +
newRow.rowIndex.toString() +
'\')">removeFilterOption</button>';
newSelectionInput = document.createElement("input");
newSelectionInput.type = "text";
newSelectionInput.name = "filterValue[]";
newSelectionInput.id = "filterValue[]";
newSelectionInput.size = 60;
newSelectionKey = document.createElement("span");
insideHTML = "<select name='filterKey[]' id='filterKey[]' class='input'>";
<?php
for ($index=0; $index < count($filterOptions); $index++) {
print " insideHTML = insideHTML + '<option>" . $filterOptions[$index] . "</option>';";
}
?>
insideHTML = insideHTML + "</select>";
newSelectionKey.innerHTML = insideHTML;
newCell.appendChild(newSelectionKey);
newCell.appendChild(newSelectionInput);
newCell.appendChild(newRemoveButton);
}
if (action.trim() == "remove") {
rmNode = document.getElementById(loc);
rmNode.parentNode.removeChild(rmNode);
}
}
//-->
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div class="loader"></div>
<form action='indextest.php' method='post'>
<table>
<tr><td width="20px"></td><td>
<?php
// var_dump($_POST);
if (isset($_POST['filterKey'])) {
$filterKey = $_POST['filterKey'];
} else {
$filterKey = array();
}
if (isset($_POST['filterValue'])) {
$filterValue = $_POST['filterValue'];
} else {
$filterValue = array();
}
if (isset($_POST['transID'])) {
$transID = $_POST['transID'];
} else {
$transID = "";
}
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else {
$action = "first";
}
if (isset($_POST['perPage'])) {
$perPage = (int) $_POST['perPage'];
} else {
$perPage = 10;
}
$result = null;
?>
<table width='100%' id="Filters">
<tr><td>FILTERS</td></tr>
<?php
if (isset( $filterKey )) {
for ($keyIndex=0; $keyIndex < count($filterKey); $keyIndex++) {
$newRow = $keyIndex + 1;
print "<tr id='Filter-" . $newRow . "'>";
print " <td>";
print " <span>";
print " <select name='filterKey[]' id='filterKey[]' class='input'>";
for ($index=0; $index < count($filterOptions); $index++) {
if ( $filterOptions[$index] == $filterKey[$keyIndex]) {
print " insideHTML = insideHTML + '<option selected>" .
$filterOptions[$index] . "</option>';";
} else {
print " insideHTML = insideHTML + '<option>" . $filterOptions[$index] . "</option>';";
}
}
print " </select>";
print " </span>";
print " <input type='text' name='filterValue[]' id='filterValue[]' size='60' value='" .
$filterValue[$keyIndex] . "'/>";
print " <span>";
print " <button type='button' onclick='filterOption(\"remove\",\"Filter-" .
$newRow . "\")'>removeFilterOption</button>";
print " </span>";
print " </td>";
print "</tr>";
}
}
?>
<tr id="Filter-0">
<td>
<button type='button' onclick="filterOption('add','Filter-0')">addFilterOption</button>
<button type='submit'>applyFilter</button>
</td>
</tr>
</table>
<table width="100%" border="0">
<thead>
<tr bgcolor="#2EFEF7">
<th width="35px">col0</th>
<th width="180px">col1</th>
<th width="80px">col2</th>
<th>col3</th>
</tr>
</thead>
<?php
?>
</table>
<?php
print " <input type='hidden' id='transID' name='transID' value='" . $transID . "'/>";
print " <table width='100%'>";
print " <tr bgcolor='#2EFEF7'>";
print " <td align='left'>";
print " <input id='action' name='action' type='submit' value='first'/>";
print " <input id='action' name='action' type='submit' value='backward'/>";
print " </td>";
print " <td align='center'>";
print " Lines per page :";
print " <select class='input' name='perPage' id='perPage'>";
for ($index=0; $index < count($numbersPerPage); $index++) {
print " <option value='" . $numbersPerPage[$index] . "'";
if ( (int) $numbersPerPage[$index] == $perPage ) {
print " selected";
}
print "/>" . $numbersPerPage[$index] . "</option>";
}
print " </select>";
print " <input id='submit' name='submit' type='submit' value='adjust'/>";
print " </td>";
print " <td align='right'>";
print " <input id='action' name='action' type='submit' value='forward'/>";
print " <input id='action' name='action' type='submit' value='last'/>";
print " </td>";
print " </tr>";
print " </table>";
?>
</td><td width="20px"></td></tr>
<table>
</form>
</body>
</html>
我添加了一些额外的代码来处理调试:
<?php
print "CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "<BR />";
$inputdata = file_get_contents('php://input');
$data = array();
parse_str($inputdata,$data);
print "DATA: <pre>";
var_dump($inputdata);
var_dump($data);
print "</pre>";
?>
这给了我几乎相同的输出:
CONTENT_TYPE: application/x-www-form-urlencoded
DATA:
string 'filterKey%5B%5D=&filterValue%5B%5D=AAAA&filterKey%5B%5D=&filterValue%5B%5D=BBBB&transID=d8018bc2-9252-dea6-e511-dd859c5c183b&perPage=10' (length=135)
array (size=4)
'filterKey' =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
'filterValue' =>
array (size=2)
0 => string 'AAAA' (length=4)
1 => string 'BBBB' (length=4)
'transID' => string 'd8018bc2-9252-dea6-e511-dd859c5c183b' (length=36)
'perPage' => string '10' (length=2)
和
CONTENT_TYPE: application/x-www-form-urlencoded
DATA:
string(135) "filterKey%5B%5D=&filterValue%5B%5D=AAAA&filterKey%5B%5D=&filterValue%5B%5D=BBBB&transID=d8018bc2-9252-dea6-e511-dd859c5c183b&perPage=10"
array(4) {
["filterKey"]=>
array(1) {
[0]=>
string(0) ""
}
["filterValue"]=>
array(1) {
[0]=>
string(4) "AAAA"
}
["transID"]=>
string(36) "d8018bc2-9252-dea6-e511-dd859c5c183b"
["perPage"]=>
string(2) "10"
}
最大的区别似乎是一个版本正在处理阵列与另一个版本略有不同(5.3显示1个条目)。
我想这是我错过的php或Apache设置或扩展。我在服务器上安装了一个默认的wampserver,它正在按预期工作,并且在OpenSuse服务器上安装了一个最小的安装,它在记住除第一个选项之外的每个选项时都有问题。