从codeigniter中的输入字段获取所有表单数据

时间:2016-06-16 07:39:30

标签: forms codeigniter

我的页面中有200个输入字段。都是5组的单选按钮。如何获取输入字段的数据。所有输入字段的名称都是从数据库动态创建的。

我的代码如下

$attributes = array('class' => 'result', 'id' => 'result', 'name' => 'result');
                        echo form_open('exam/result',$attributes);
                    $a = array(
                            'name'          => 'aptitude-'.$value->question_id,
                            'id'            => 'answer1',
                            'value'         => 'a',
                            'checked'       => FALSE,
                        );
                    $b = array(
                            'name'          => 'aptitude-'.$value->question_id,
                            'id'            => 'answer2',
                            'value'         => 'a',
                            'checked'       => FALSE,
                        );
                    $c = array(
                            'name'          => 'aptitude-'.$value->question_id,
                            'id'            => 'answer3',
                            'value'         => 'a',
                            'checked'       => FALSE,
                        );
                    $d = array(
                            'name'          => 'aptitude-'.$value->question_id,
                            'id'            => 'answer4',
                            'value'         => 'a',
                            'checked'       => FALSE,
                        );
                    $e = array(
                            'name'          => 'aptitude-'.$value->question_id,
                            'id'            => 'answer5',
                            'value'         => 'a',
                            'checked'       => FALSE,
                        );
                    $f = array(
                            'name'          => 'aptitude-'.$value->question_id,
                            'id'            => 'answer6',
                            'value'         => 'a',
                            'checked'       => TRUE,
                            'style'         => 'display:none'
                        );

                    echo form_radio($a); 
                    echo form_label($value->a, 'a');
                    echo "<br>";

                    echo form_radio($b); 
                    echo form_label($value->b, 'b');
                    echo "<br>";

                    echo form_radio($c); 
                    echo form_label($value->c, 'c');
                    echo "<br>";

                    echo form_radio($d); 
                    echo form_label($value->d, 'd');
                    echo "<br>";

                    echo form_radio($e); 
                    echo form_label($value->e, 'e');
                    echo "<br>";

                    echo form_radio($f); 


                    echo form_close();

此代码将运行200次

1 个答案:

答案 0 :(得分:1)

使用数组名称$a = array( 'name' => 'aptitude['.$value->question_id.']', 'id' => 'answer1', 'value' => 'a', 'checked' => FALSE, ); $b = array( 'name' => 'aptitude['.$value->question_id.']', 'id' => 'answer2', 'value' => 'b', 'checked' => FALSE, ); $c = array( 'name' => 'aptitude['.$value->question_id.']', 'id' => 'answer3', 'value' => 'c', 'checked' => FALSE, ); $d = array( 'name' => 'aptitude['.$value->question_id.']', 'id' => 'answer4', 'value' => 'd', 'checked' => FALSE, ); $e = array( 'name' => 'aptitude['.$value->question_id.']', 'id' => 'answer5', 'value' => 'e', 'checked' => FALSE, ); $f = array( 'name' => 'aptitude['.$value->question_id.']', 'id' => 'answer6', 'value' => 'f', 'checked' => TRUE, 'style' => 'display:none' );

$aptitudes = $this->input->post('aptitude');

print_r($aptitudes);

// example output
[7] => a
[5] => c
[3] => f
// where [7] [5] [3] are the $value->question_id

获取POST值:

$aptitudes[5]; // output => c

获取特定的帖子

if(mysqli_query($test_db , $sql)){
echo '
    <script type="text/javascript">
        alert("ID of last inserted row: '.mysqli_insert_id($test_db).'"); 
        </script>'; 
}