0

I have 200 input field in my page. All are radio button of group of 5. How can I get the data of input field. Names of all input field are created dynamically from database.

My code is as follows

$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();

This code will run for 200 times

1 Answer 1

1

Use array name aptitude[]:

$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'
    );

Get POST value:

$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

Get specific POST

$aptitudes[5]; // output => c
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.