多数组表单不会存储变量

时间:2016-03-25 14:07:44

标签: php arrays forms multidimensional-array

我有一个提交update.php的表单,它应该接受POST变量并将它们存储在另一个文件中

形式:

<fieldset>
        <legend>Proficiencies:</legend>
        <span>Name:</span> <span>exp:</span> <span>Rank:</span><br/>
        <?php
             $x = 0;
             if ($proficiencies) {
                foreach ($proficiencies as $proficiency) {
                        echo '<input type="text" name="proficiencies['.$x.'][\'name\']" value="'.$proficiency["name"].'"/>';
                        echo ' <input type="text" name="proficiencies['.$x.'][\'exp\']" value="'.$proficiency["exp"].'"/>';
                        echo ' <input type="text" name="proficiencies['.$x.'][\'rank\']" value="'.$proficiency["rank"].'"/>';
                        echo '<br/>';
                        $x++;
               }
            }
           echo '<input type="text" name="proficiencies['.$x.'][\'name\']"/>';
           echo ' <input type="text" name="proficiencies['.$x.'][\'exp\']"/>';
           echo ' <input type="text" name="proficiencies['.$x.'][\'rank\']"/>';
       ?>

</fieldset>

update.php:

<?php
echo 'making changes';
$user = fopen('sheets/'.$_COOKIE['username'].'.php', 'w+');
fwrite ($user, '<?php
$proficiencies = array(
');
foreach ($_POST['proficiencies'] as $proficiency) {
    if ($proficiency["name"]) {
        fwrite ($user, '    array(
        "name" => "'.$proficiency["name"].'",
        "exp" => "'.$proficiency["exp"].'",
        "rank" => "'.$proficiency["rank"].'",
    ),
'
        );
    }
}
fwrite ($user, ');
?>');
fclose($user);
?>

我尝试了很多不同的方法,但它不会起作用

1 个答案:

答案 0 :(得分:0)

在html表单名称中不允许使用引号

变化

echo '<input type="text" name="proficiencies['.$x.'][\'name\']" value="'.$proficiency["name"].'"/>';
echo ' <input type="text" name="proficiencies['.$x.'][\'exp\']" value="'.$proficiency["exp"].'"/>';
echo ' <input type="text" name="proficiencies['.$x.'][\'rank\']" value="'.$proficiency["rank"].'"/>';

要:

echo '<input type="text" name="proficiencies['.$x.'][name]" value="'.$proficiency["name"].'"/>';
echo ' <input type="text" name="proficiencies['.$x.'][exp]" value="'.$proficiency["exp"].'"/>';
echo ' <input type="text" name="proficiencies['.$x.'][rank]" value="'.$proficiency["rank"].'"/>';

应该没问题

相关问题