我从动态表单 (HTML+JavaScript) 获取输入并使用 PHP 脚本提取数据。我正在使用serialize
PHP 中的语句。这似乎不起作用 - 我无法将任何数据放入变量$featureids
和$featurenames
.
这是代码 -
HTML 部分 -
<table border = "1px" width = "700px">
<tr width> <td>
<b> Features </b> <br/> <br/>
FeatureId: <input type = "text" name = "FirstFeatureId"> <br/>
FeatureName: <input type = "text" name = "FirstFeatureName">
<input type = "button" value = "Add Item" onclick = "addFeatureItem()" /> <br/>
<span id = "features"> <br/> </span>
</td> </tr>
</table>
JavaScript 方法
function addFeatureItem(){
//Text field to get the feature id
var featureIdTextField = document.createElement("input");
featureIdTextField.setAttribute("name", "FeatureId[]");
featureIdTextField.setAttribute("value", "Feature Id");
var item = document.getElementById("features");
item.appendChild(featureIdTextField);
//Text field to get the feature name
var featureNameTextField = document.createElement("input");
featureNameTextField.setAttribute("name", "FeatureName[]");
//Insert a break statement after creating a feature id and name
var breakStatement = document.createElement("br");
item.appendChild(featureNameTextField);
item.appendChild(breakStatement);
}
和 PHP 部分 -
$featureids = serialize($_POST["FeatureId"]);
echo $featureids;
$featureids = unserialize($featureids);
//inserting all the feature ids into an array
$featureIdArray = array($_POST["FirstFeatureId"]);
foreach ($featureids as $featureid) {
echo $featureid ;
array_push($featureIdArray, $featureid);
}
$featurenames = serialize($_POST["FeatureName"]);
$featurenames = unserialize($featurenames);
//insert all the feature names into an array
$featureNameArray = array($_POST["FirstFeatureName"]);
foreach ($featurenames as $featurename) {
array_push($featureNameArray, $featurename);
}
编辑- 这是 Chrome 中 Networks->Headers 中的表单数据
FirstFeatureId:fid
FirstFeatureName:fname
FeatureId[]:Feature Id
FeatureName[]:Feature Name
FeatureId[]:Feature Id
FeatureName[]:Feature Name
FeatureId:
注意 - 由于某种原因,最后一个 FeatureId 为空