【问题标题】:multi array form won't store variables多数组形式不会存储变量
【发布时间】:2016-03-25 14:07:44
【问题描述】:

我有一个提交给 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>

更新.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);
?>

我已经尝试了很多不同的方法,但都行不通

【问题讨论】:

    标签: php arrays forms multidimensional-array


    【解决方案1】:

    在 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"].'"/>';
    

    应该没问题

    【讨论】:

    • 更正:您可以在表单名称中使用引号,但它会像名称一样携带,因此在 $_POST['proficiencies'] 中应该有 $ 之类的键proficiency["'name'"] - 注意双引号内的单引号:P
    猜你喜欢
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    相关资源
    最近更新 更多