【问题标题】:form input name from mysql来自mysql的表单输入名称
【发布时间】:2015-01-15 17:26:43
【问题描述】:

我使用 php 和 mysql 创建了一个调查系统。这里的问题是当我单击提交按钮时,我不知道如何传递“输入名称”。

例如:

 1. Test 1
 <input type="text" name="name_{=$res[$a]['id']}">

 2. Test 2
 <input type="text" name="name_{=$res[$a]['id']}">

如何调用“输入名称”?

 if(isset($_post['btnSubmit'])) {
    $xxx = $_post['name_????'];
    $yyy = $_post['name_????'];
 }

我用 {} 替换打开/关闭 php 标签,因为它不允许..

编辑

    <div class="form-body">
                    <h3 class="block"><?=$resSurvey[0]['survey_title']?></h3>
                    <p><?=$resSurvey[0]['survey_desc']?></p>
                    <br><br>

                    <?php
                        if ($rowQuestion >0) {
                            $bil=1;
                            for ($a=0; $a<$rowQuestion; $a++) {
                    ?>
                    <div class="form-group">
                        <label class="control-label col-md-3"><?=$bil?>. <?=$resQuestion[$a]['question_title']?></label>
                        <?php if ($resQuestion[$a]['question_type'] == 'tf') { ?>
                        <div class="radio-list col-md-4" >
                            <label class="radio-inline"><input type="radio" name="name_<?=$resQuestion[$a]['question_id']?>" value="t"> True </label>
                            <label class="radio-inline"><input type="radio" name="name_<?=$resQuestion[$a]['question_id']?>" value="f"> False </label>
                        </div>
                        <?php } ?>

                        <?php if ($resQuestion[$a]['question_type'] == 'st') { ?>
                        <div class="col-md-4">
                            <input type="text" name="name_<?=$resQuestion[$a]['question_id']?>" class="form-control" placeholder="Enter text">
                        </div>
                        <?php } ?>

                        <?php if ($resQuestion[$a]['question_type'] == 'lt') { ?>
                        <div class="col-md-4">
                            <textarea class="form-control" name="name_<?=$resQuestion[$a]['question_id']?>" rows="3"></textarea>
                        </div>
                        <?php } ?>

                        <?php 
                            if ($resQuestion[$a]['question_type'] == 'ms') {
                                $sqlOption = sprintf("select * from tbl_surveychoices where question_id=%d", mysql_real_escape_string($resQuestion[$a]['question_id']));
                                $resOption = selectSQL($sqlOption);
                                $rowOption = count($resOption);
                        ?>
                        <div class="radio-list col-md-4">
                            <?php for($b=0; $b<$rowOption; $b++) { ?>
                            <label class="radio"><input type="radio" name="name_<?=$resQuestion[$a]['question_id']?>" value="<?=$resOption[$b]['choices_id']?>"> <?=$resOption[$b]['choices_title']?></label>
                            <?php } ?>
                        </div>
                        <?php } ?>

                        <?php 
                            if ($resQuestion[$a]['question_type'] == 'mm') {
                                $sqlOption = sprintf("select * from tbl_surveychoices where question_id=%d", mysql_real_escape_string($resQuestion[$a]['question_id']));
                                $resOption = selectSQL($sqlOption);
                                $rowOption = count($resOption);
                        ?>
                        <div class="radio-list col-md-4">
                            <?php for($b=0; $b<$rowOption; $b++) { ?>
                            <label><input type="checkbox" name="name_<?=$resQuestion[$a]['question_id']?>[]" value="<?=$resOption[$b]['choices_id']?>">  <?=$resOption[$b]['choices_title']?> </label>
                            <?php } ?>
                        </div>
                        <?php } ?>
                    </div>
                    <?php
                                $bil++;
                            }
                        }
                    ?>
                </div>

【问题讨论】:

  • 我们怎么知道?,我们不知道 $res[$a]['id'] 里面是什么。
  • 你没有帮助@KoenHoeijmakers
  • 好吧,也许可以给我们提供整个代码?如果你不知道$res[$a]['id']里面是什么,那我们怎么可能知道呢?
  • 即时编辑问题..也许会有所帮助

标签: php mysql forms input


【解决方案1】:

我会让自己更轻松,并让输入名称的索引为 ???而是

 name="name_<?=$resQuestion[$a]['question_id']?>[]"

变成

name="name['<?=$resQuestion[$a]['question_id']?>'][]"

然后在提交时就变成了

if(isset($_post['btnSubmit'])) {
   foreach ($_POST['name'] as $question_id => $values) {
      $xxx[$question_id] =  $values;
   }
}

【讨论】:

    【解决方案2】:
    $inside = 'name_'.$res[$a]['id'];    
    $xxx = $_POST[$inside];
    

    试试这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-31
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 2013-06-14
      相关资源
      最近更新 更多