PHP序列化不起作用

时间:2013-05-16 01:52:30

标签: php javascript

我从动态表单(HTML + JavaScript)获取输入并使用PHP脚本提取数据。我在PHP中使用serialize语句。这似乎不起作用 - 我无法将任何数据输入变量$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);
}

编辑 - 这是Networks-&gt; Chrome中的标题

中的表单数据
FirstFeatureId:fid
FirstFeatureName:fname
FeatureId[]:Feature Id
FeatureName[]:Feature Name
FeatureId[]:Feature Id
FeatureName[]:Feature Name
FeatureId:

注意 - 由于某种原因,最后一个FeatureId为空

1 个答案:

答案 0 :(得分:0)

我认为问题出在这附近

var featureIdTextField = document.createElement("input");
featureIdTextField.setAttribute("name", "FeatureId[]");
featureIdTextField.setAttribute("value", "Feature Id");

尝试记录这些FeatureId和FirstFeatureName,我确定这些是空的

其他方面,序列化和不实现函数没有问题。

另外,为什么不使用jQuery创建新元素