如何在codeigniter中使用表单生成器?

时间:2016-11-18 11:03:46

标签: php codeigniter

<?
   $form_options =    array(

         array(/* Name */
                 'id' => 'name',
                 'placeholder' => 'Please Enter Name',
                 'name' => 'name',
                 'type' =>'text',
                 'label' => 'Name:'
         ),
         array(/* Email */
                 'id' => 'email',
                 'placeholder' => 'Please Enter Email',
                 'name' => 'email',
                 'type' =>'email',
                 'label' => 'Email Id:'
         ),
          array(/* Password */
                 'id' => 'password',
                 'placeholder' => 'Please Enter Password',
                 'name' => 'password',
                 'type' =>'password',
                 'label' => 'Password:'
         ),
         array(/* contact */
                 'id' => 'contact',
                 'placeholder' => 'Please Enter Contact',
                 'name' => 'contact',
                 'type' =>'text',
                 'label' => 'Contact:'
         ),


         array(/* Gender */
                 'id' => 'gender',
                 'label' => 'Gender',
                 'type' => 'radio',
                 'options' => array(
                         array(
                                 'id' => 'gender',
                                 'value' => 'male',
                                 'label' => 'Male'
                         ),
                         array(
                                 'id' => 'gender',
                                 'value' => 'female',
                                 'label' => 'Female'
                         )
                 )
         ),
          array(/* Address */
                 'id' => 'address',
                 'type' => 'textarea',
                 'placeholder' => 'Address'
         ),
         array(/* SUBMIT */
                 'id' => 'submit',
                 'type' => 'submit'
         )
     );

   echo $this->form_builder->build_form_horizontal($form_options);
   ?>

1 个答案:

答案 0 :(得分:0)

我的$form_options数组中有一些变化。

你的控制器:

public function myForm()
{
    $this->load->helper('form');

    $form_options = array(
             array(/* Name */
                     'id' => 'name',
                     'placeholder' => 'Please Enter Name',
                     'name' => 'name',
                     'type' =>'text',
                     'label' => 'Name: '
             ),
             array(/* Email */
                     'id' => 'email',
                     'placeholder' => 'Please Enter Email',
                     'name' => 'email',
                     'type' =>'email',
                     'label' => 'Email Id: '
             ),
              array(/* Password */
                     'id' => 'password',
                     'placeholder' => 'Please Enter Password',
                     'name' => 'password',
                     'type' =>'password',
                     'label' => 'Password: '
             ),
             array(/* contact */
                     'id' => 'contact',
                     'placeholder' => 'Please Enter Contact',
                     'name' => 'contact',
                     'type' =>'text',
                     'label' => 'Contact: '
             ),


             array(/* Gender */
                     'id' => 'gender',
                     'label' => 'Gender : ',
                     'name' => 'Gender',
                     'type' => 'radio',
                     'options' => array(
                             array(
                                     'id' => 'gender',
                                     'value' => 'male',
                                     'label' => 'Male',
                                     'name' =>  'gender',
                             ),
                             array(
                                     'id' => 'gender',
                                     'value' => 'female',
                                     'label' => 'Female',
                                     'name' =>  'gender',
                             )
                     )
             ),
              array(/* Address */
                     'id' => 'address',
                     'type' => 'textarea',
                     'placeholder' => 'Address',
                     'label'    =>  ' Address',
                     'name' => 'address',
             ),
             array(/* SUBMIT */
                     'id' => 'submit',
                     'type' => 'submit',
                     'label' => 'Submit',
                     'name' => 'submit',

             )
     );

    $data['myForm'] = $form_options;
    $this->load->view("welcome/test", $data);
}

在您的视图文件中:

<table>
<?php
echo form_open('user/newuser');
    foreach($myForm as $key => $val) {
        echo "<tr>";
        if($val['type'] == 'textarea') 
        {
            echo "<td>".$val['label']."</td>";
            echo "<td>".form_textarea($val)."</td>";
        } 
        elseif($val['type'] == 'radio') 
        {
            echo "<td></td>";
            echo "<td>";
            foreach($val['options'] as $radio) {
                echo form_radio($radio);
                echo $radio['label'];
            }       
            echo "</td>";
        } 
        elseif($val['type'] == 'submit') 
        {
            echo "<td></td>";
            echo "<td>".form_submit($val['name'], $val['label'])."</td>";
        } 
        else 
        {
            echo "<td>".$val['label']."</td>";
            echo "<td>".form_input($val)."</td>";
        }
        echo "<tr>";
    }
echo form_close();
?>
</table>

输出:

Form Output