提交表单发布为ARRAY,似乎无法设置要使用的变量

时间:2014-12-15 16:49:27

标签: php arrays forms

我试图在一个表单上提交多行。 值的命名约定为form[ValueName][]

$rows = 2;
for($i = 0; $i <= $rows - 1; $i++){


    echo "<tr><td align=left><Select class='required' name=form[PlanType][]>" . $PlanList . "</select></td>";
    echo "<td align=left><Input class='required' style='max-width:455px;' name='form[PlanDescr][]' value='" . $PlanDescr . "'></td>";
    echo "<td align=left><select class='required' name='form[EmployeeId][]'>" . $dd . "</select></td>";
    echo "<td align=left><input class='required' type=text style='text-align:right;max-width:100px;' name='form[RepNum][]' value='" . $RepNum . "'></td>";
    echo "<td align=left><input class='required' type=text style='text-align:right;max-width:100px;' name='form[OfficeNum][]' value='" . $OfficeNum . "'></td>";
    echo "<td align=center><input type=checkbox name='form[Main][]' value='1' " . ($main?"checked":"") . "></td>";
    echo "<td align=left><select class='required' name='form[Schedule][]'>" . $Schedule . "</select></td>";
    echo "<td align=left><input class='required' type=text style='text-align:right;max-width:100px;' name='form[NumMos][]' value='" . $NumMos . "'></td>";

    echo "<td align=left><input class='required' type=text style='text-align:right;max-width:100px;' name='form[PctPmt][]' value='" . $PctPmt . "'></td>";

    echo "<td align=left><input class='required' type=text style='text-align:right;max-width:100px;' name='form[FrontPmt][]' value='" . $FrontPmt . "'></td></tr><tr class='spacer'></tr>";
}

我保存到数据库并发送到API。 当它只是一行作为一个简单的POST它工作正常,但尝试提交多个数组时,我在调试期间尝试回显每个值时返回一个空数组。

提交功能中的变量设置:

$count = count($_REQUEST['form']['PlanType']);



for($i = 0; $i <= $count - 1; $i++){
    //Pull, Sanitize, and Set URL Variables
    $residualsClient = new ResidualsAPI();
    $mydb = DBManagerFactory::getInstance("formdbconfig");
    $EmployeeId = filter_input_array($_POST["form"]["EmployeeId"][$i],FILTER_SANITIZE_STRING);
    $PlanType = filter_input_array($_POST["form"]["PlanType"][$i],FILTER_SANITIZE_STRING);
    $PlanDescr = filter_input_array($_POST["form"]["PlanDescr"][$i],FILTER_SANITIZE_STRING);
    $Schedule = filter_input_array($_POST["form"]["Schedule"][$i],FILTER_SANITIZE_STRING);
    $FrontPmt = filter_input_array($_POST["form"]["FrontPmt"][$i],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    $PctPmt = filter_input_array($_POST["form"]["PctPmt"][$i],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    $OfficeNum = filter_input_array($_POST["form"]["OfficeNum"][$i],FILTER_SANITIZE_NUMBER_INT);
    $RepNum = filter_input_array($_POST["form"]["RepNum"][$i],FILTER_SANITIZE_NUMBER_INT);
    $NumMos = filter_input_array($_POST["form"]["NumMos"][$i],FILTER_SANITIZE_NUMBER_INT);
    $PlanId = filter_input_array($_POST["form"]["PlanId"][$i],FILTER_SANITIZE_NUMBER_INT);
}

我似乎无法弄清楚我做错了什么? 我错过了一些愚蠢而简单的东西吗?

先谢谢,

史蒂芬

2 个答案:

答案 0 :(得分:1)

输入名称属性中不需要第二个空[],只需要[name]

答案 1 :(得分:1)

删除空[],即name ='form [Schedule]'。然后从$ _POST [“form”]获得关联数组。

$Schedule = filter_var($_POST["form"]["Schedule"],FILTER_SANITIZE_STRING);