doueta6642 2013-03-20 13:31 采纳率: 0%
浏览 20
已采纳

表单$ _POST错误,动态字段

I have an error that I can't figure out...

Om my webpage there is a form that the user has the ability to add some new input fields to. If the user is submitting the form, then the optional fields is empty when the php-file is handing them, why?

HTML:

        <form method="post" action="newRequest.php">
            <input type="text" name="title" />
            <input type="hidden" name="fname" value="0" />
            <input type="checkbox" name="fname" value="1"/>
            <input type="hidden" name="ename" value="0" />
            <input type="checkbox" name="ename" value="1" />
            <input type="hidden" name="seat" value="0" />
            <input type="checkbox" name="seat" value="1" />
            <input type="hidden" name="fields" value="0" />
            <input type="text" id="fields" name="fields" />
            <input type="submit" />
        </form>

PHP:

if (strlen($_POST[title]) > 2) {
    $toDb[title] = $_POST[title];
} else {
    error('title');
}

$toDb[fname] = $_POST[fname];
$toDb[ename] = $_POST[ename];
$toDb[seat] = $_POST[seat];

if ($_POST[fields] > 0) {
    $i = 0;
    while ($i < $_POST[fields]) {
        $toDb[optional][$i] = $_POST[optional-$i];
        $i++;
    }
    $toDb[optional] = serialize($toDb[optional]);
} else {
    $toDb[optional] = 0;
}
newEvent($toDb,$dbh);

JQuery that is adding dynamical fields:

$(document).ready(function() {
    $('#fields').focusout(function(){

        var fields = $('#fields').val();
        var i = 0;

        while(i < fields) {
            $('#fields').after("Valfritt fält "+(i+1)+":<input type='text' name='optional"+i+"' />");
            i++;
        }

    })
})
  • 写回答

3条回答 默认 最新

  • douzong3599 2013-03-20 13:38
    关注

    You should quote array indexes. It should be

     $toDb['optional'][$i] = $_POST['optional'.$i];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?