POST值未按预期返回

时间:2013-06-19 04:29:50

标签: php javascript forms post

我有一张表格(发票),附有一组输入(收费)。

<tr>
    <td><label>Description:</label></td>
    <td><input type="text name="description[]" value="'.$labor->description.'" /></td>
    <td><label>Hours:</label></td>
    <td><input type="text name="hours[]" value="'.$labor->hours.'" size="2" /></td>
    <td><label>Rate:</label></td>
    <td><input type="text name="rate[]" value="'.$labor->rate.'" size="2" /></td>
    <input type="hidden" name="labor_id[]" value="'.$labor->id.'" />
</tr>

我正在使用javascript函数添加另一组这些输入。

function moreLabor(table) {

    var table = document.getElementById(table);
    var row = table.insertRow(-1);

    var descriptionLabel = document.createElement("label");
    descriptionLabel.innerHTML="Description:";
    var description = document.createElement("input");
    description.name = "description[]";
    description.type = "text";

    var td1 = row.insertCell(-1);
    var td2 = row.insertCell(-1);
    td1.appendChild(descriptionLabel);
    td2.appendChild(description);

    var hoursLabel = document.createElement("label");
    hoursLabel.innerHTML="Hours:";
    var hours = document.createElement("input");
    hours.name = "hours[]";
    hours.type = "text";
    hours.size = 2;
    hours.value = 1;
    var td1 = row.insertCell(-1);
    var td2 = row.insertCell(-1);
    td1.appendChild(hoursLabel);
    td2.appendChild(hours);

    var rateLabel = document.createElement("label");
    rateLabel.innerHTML="Rate:";
    var rate = document.createElement("input");
    rate.name = "rate[]";
    rate.type = "text";
    rate.size = 2;
    rate.value = 25;

    var td1 = row.insertCell(-1);
    var td2 = row.insertCell(-1);
    td1.appendChild(rateLabel);
    td2.appendChild(rate);

    var remove = document.createElement("input");
    remove.type = "button";
    remove.value = "Remove";
    remove.onclick = function () {
        var parent = this.parentNode.parentNode; //get the row node
        table.deleteRow(parent.rowIndex); //Delete the row index behind it.
    };
    var td1 = row.insertCell(-1).appendChild(remove);

}

如您所见,我在重复输入名称后使用[]。问题是POST只返回每个值的最后一组。我查看了原始查询字符串,它也只包含最后一组值。速率,小时和描述作为数组返回,但每个只设置一个值。如何填充整个POST数组?

3 个答案:

答案 0 :(得分:2)

看一下您的语法,您已合并typename

 type="text name="hours[]"

这应该是

 type="text" name="hours[]"

答案 1 :(得分:0)

男人,我觉得自己像个白痴。当我看到带有彩色注释的问题时,我注意到在type =“text

之后缺少了”s

答案 2 :(得分:0)

<tr>
    <td><label>Description:</label></td>
    <td><input type="text name="description[]" value="'.$labor->description.'" /></td>
    <td><label>Hours:</label></td>
    <td><input type="text name="hours[]" value="'.$labor->hours.'" size="2" /></td>
    <td><label>Rate:</label></td>
    <td><input type="text name="rate[]" value="'.$labor->rate.'" size="2" /></td>
    <input type="hidden" name="labor_id[]" value="'.$labor->id.'" />
</tr>


should be

<tr>
    <td><label>Description:</label></td>
    <td><input type="text" name="description[]" value="'.$labor->description.'" /></td>
    <td><label>Hours:</label></td>
    <td><input type="text" name="hours[]" value="'.$labor->hours.'" size="2" /></td>
    <td><label>Rate:</label></td>
    <td><input type="text" name="rate[]" value="'.$labor->rate.'" size="2" /></td>
    <input type="hidden" name="labor_id[]" value="'.$labor->id.'" />
</tr>