0

Im developing form application. Inside the form there are textboxes,textarea, checkboxes checkboxes are populated according to array. and i pass all the form values to controller . Values of textboxes, texarea print correctly. Problem is there only prints last value of checked checkbox. how do i print all checked box values. please help me & please find the code i used.

askQuestion.php (view)

    <?php echo form_open('homepage/test'); ?>              

    <p>
    <div>
            <div class="form-group">
        Question Title:<br/>

        <input type="text" value="" name="">
        </p>
    <div>

   <div class="form-group">
    <p>
        Description: <br/>

        <textarea name="decription" rows="5" cols="100"> </textarea>
    </p>

     <?php

     $chk_group =array('1' => 'red',
                           '2' => 'aa',
         '3' => 'bb',
         '4' => 'cc',
         '5' => 'dd' 

     );

     var_dump($chk_group);



     for ($i=1 ; $i<=count($chk_group);$i++)
     {

         $val =$chk_group[$i];
         echo "<br>";
         echo '<input type="checkbox" value="' . $val . '" name="chk_group">' . $val;
         echo "</br>";

     }

     ?>


     </div>

            <div class="form-group">
       Declare new Tags:<br/>

        <input type="text" value="" name="tag">
        </p>
    </div>

    <p>
        <input type="submit"  class="btn btn-success btn-block" value="Post Your Question" id="postQuestion">
    </p>
    <?php echo form_close();?>

homepage.php (controller)

public function test() {
        echo "test";
        $name = $this->input->post('tag');
        print_r($name);

        $des = $this->input->post('decription');
        print_r($des);

        $data = $this->input->post('chk_group');

        var_dump($data);

      /*  foreach ($this->input->post('chk_group') as $r) {
            echo $r;
        }
*/

    }

1 Answer 1

1

You should use array to name the check boxes. You used loop to generate the check boxes & used same name for all. For this you got only last one value.

echo '<input type="checkbox" value="' . $val . '" name="chk_group[]">' . $val;
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.