我有自定义窗体循环,无线电值是覆盖

时间:2016-02-08 15:17:58

标签: php html html5

我的数据库上有这个: enter image description here

然后这是在我的后端

<?php 
    foreach($form['questions'] as $question) :

        $options = $question['options'];
        $options = explode(',',$options);
?>

        <div class="form-group">
            <input type="hidden" value="<?php echo $question['label']; ?>" name="custom_form[label][]" />
            <label><?php echo $question['label']; ?></label>
            <?php if($question['type'] == 'text'): ?>
                <input type="text" placeholder="<?php echo $question['label']; ?>" name="custom_form[answer][]" class="form-control" required />
            <?php elseif($question['type'] == 'textarea'): ?>
                <textarea name="custom_form[answer][]" placeholder="<?php echo $question['label']; ?>" class="form-control" required></textarea>
            <?php elseif($question['type'] == 'radio'): ?>

                <div class="input-group btn-group" data-toggle="buttons">
                    <?php foreach($options as $option): ?>
                        <label class="btn btn-default"><input type="radio" name="custom_form[answer][]" value="<?php echo $option; ?>" required><?php echo $option; ?></label>
                    <?php endforeach; ?>
                </div>
            <?php elseif($question['type'] == 'select'): ?>
                <select class="form-control" name="custom_form[answer][]" required>
                    <option value=""></option>
                    <?php foreach($options as $option): ?>
                        <option><?php echo $option; ?></option>
                    <?php endforeach; ?>
                </select>
            <?php endif; ?>
        </div>
<?php
    endforeach;
?>

这是PHP的结果,然后当我通过新radio

提交重叠时 提交前

enter image description here

提交后

enter image description here

**你可以看到,第二台无线电已经消失了**

编辑添加了html

<div class="col-md-6">
    <div class="form-group">
        <input type="hidden" name="custom_form[label][]" value="What is your current Immigration status?">
        <label>What is your current Immigration status?</label>
        <select required="" name="custom_form[answer][]" class="form-control">
                <option value=""></option>
                <option>Naturalized Citizen</option>
                .....
        </select>
    </div>

    <div class="form-group">
        <input type="hidden" name="custom_form[label][]" value="Do you have VISA?">
        <label>Do you have VISA?</label>

        <div data-toggle="buttons" class="input-group btn-group">
            <label class="btn btn-default active"><input type="radio" required="" value="yes" name="custom_form[answer][]">yes</label>
            <label class="btn btn-default"><input type="radio" required="" value="no" name="custom_form[answer][]">no</label>
        </div>
    </div>

    <div class="form-group">
        <input type="hidden" name="custom_form[label][]" value="Which type of VISA do you currently have?">
        <label>Which type of VISA do you currently have?</label>
        <select required="" name="custom_form[answer][]" class="form-control">
            <option value=""></option>
            <option>A-1/A-2</option>
            .....
        </select>
    </div>

    <div class="form-group">
        <input type="hidden" name="custom_form[label][]" value="What is your gender?">
        <label>What is your gender?</label>

        <div data-toggle="buttons" class="input-group btn-group">
            <label class="btn btn-default active"><input type="radio" required="" value="male" name="custom_form[answer][]">male</label>
            <label class="btn btn-default"><input type="radio" required="" value="female" name="custom_form[answer][]">female</label>
        </div>
    </div>
</div>

EDIT2

当我现在使用时,我发现undefined offset错误

$custom_forms = $_POST['custom_form'];
foreach($custom_forms['label'] as $key => $question) {
    echo htmlentities($question . ' - ' . $custom_forms['answer'][$key]) . '<br>';
}

1 个答案:

答案 0 :(得分:0)

<input type="radio" name="custom_form[answer][]" />

据我所知,您输入的名称相同。

因为它是一个数组,所以不会打扰文本输入。

但是单选按钮的工作方式不同,你有两组具有相同名称的单选按钮,所以虽然css将它们显示为两个sepparate实体,但是当你提交表单时,它将被解析为只有一个单选按钮组。

我的解决方案是将无线电输入的名称更改为

"custom_form[answer][input_<?php=$question['id']?>]"