【问题标题】:form_dropdown() is not taking the "name" attribute in codeigniterform_dropdown() 没有在 codeigniter 中使用“name”属性
【发布时间】:2016-07-01 13:41:06
【问题描述】:

我正在使用 form 辅助类在 codeigniter 中创建一个表单。

但是使用 'form_dropdown("medium_type",$medium_type,"Select Medium")' 并没有使用 name 属性,因此表单验证在发布后不起作用。

谁能告诉我如何将名称传递给元素。

function controller(){
  $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            $this->data['medium_type']  = $this->field_enums('notifications', 'medium_type');
             $this->data['user_type'] = $this->field_enums('notifications', 'user_type');
                            $this->form_validation->set_rules('medium_type', 'Medium Type', 'required');

            if ($this->form_validation->run() == FALSE)
            {
                    $this->load->view('admin/notifications', $this->data);
            }
            else
            {
                    $data = array(
                    'title' => $this->input->post('title'),
                    'medium_type' => $this->input->post('mediun_type'),
                    );
                    $this->notifications_m->insert($data);

            }
            $this->load->view('admin/notifications', $this->data);
}

表单代码 ` 标题

                        <div class="input-field col s12 m6 l6 margin-bottom-10px">
                          <?php echo form_dropdown("medium_type",$medium_type);?>
                          <label for="medium_tyoe">Medium Type</label>
                          <?php echo form_error('medium_type'); ?>
                        </div>

                    <div class="input-field col s12 m12 l12 margin-bottom-10px">
                          <?php echo $this->ckeditor->editor("content");?>
                          <?php echo form_error('content'); ?>
                        </div>

                        <div class="btn waves-effect waves-light col s8 m4 l4 offset-m4 offset-l4 offset-s2 ">
                          <?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
                    <i class="material-icons right">send</i>
                        </div>
                      </div>
                    </div>
                <?php echo form_close();?>`

【问题讨论】:

  • 这里你的下拉列表有name="medium_type"
  • 同时显示您的控制器代码
  • 加上它是枚举值的下拉列表,我如何在该助手中传递空的数组字段(“”=>“选择介质类型”)?

标签: codeigniter post form-helpers


【解决方案1】:

在表格中

$options = array(
        'option'         => 'Name',
        'option2'         => 'Name2',
);

echo form_dropdown('medium_type', $options, 'option2');
echo form_error('medium_type');  # to show error

在控制器中

$this->form_validation->set_rules('medium_type', 'Medium Type', 'required');

if ($this->form_validation->run() == FALSE)
{
    # load the form view
}
else
{
    # Validation  pass
    $medium_type = $this->input->post('medium_type');

    echo $medium_type ;
}

【讨论】:

  • 这正是我使用的方式。但是在浏览器控制台中我找不到输入字段“medium_type”的名称属性
  • @DeepikaG 控制台??你想做什么??
  • 好的,让我更正一下,我需要将空的“选择媒体类型”传递给下拉菜单,1.我们如何在 form_dropdown 中做到这一点,2.一旦完成,因为 name 属性是未添加,验证是否有效?
  • 显示您的表单代码。上面的代码运行良好。我很确定
  • $medium_type 数据在哪里??
猜你喜欢
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
相关资源
最近更新 更多