dongyue110702 2015-12-10 10:21
浏览 27

在CodeIgniter中验证失败后,从数据库生成复选框

I'm creating a form with validation in CodeIgniter, and I have problem with mixing proccessing form with validation. In my form I have checkboxes that are generated from database. All works, when someone submit form without mistake (no validation failure), but after failed validation my checkboxes are not generated correctly (I have errors).

My Controller:

    public function form()
{
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    /////////////////////////////////////////
    ////// MY PROCESSING CODE //////////////
    ////////////////////////////////////////
    //Generating checkbocks from database
    $data['szkolenia'] = $this->Szkolenie_m->pobierz();

    //Validation
    $this->form_validation->set_rules('imie', 'Imię', 'required');
    $this->form_validation->set_message('required', 'Błąd: wypełnij powyższe pole');

    //After form submit
            if (!empty($_POST))
    {
    $konsultant = $this->uri->segment(3);

    $dane = array(
        'email1' => $this->input->post('email'),
        'imie' => $this->input->post('imie'),
        'nazwisko' => $this->input->post('nazwisko'),
        'nazwa_firmy' => $this->input->post('firma'),
        'konsultant_id' => $konsultant,
    );

    //Saving selected checkboxes into another db
    $boxes = $_POST['formChecks'];
    $N = count($boxes);
    $ostatni_id = $this->Osoby_m->ostatni_id();

    for($i=0; $i < $N; $i++)
    {
        $this->Osoby_m->nowy_wpis_formularz($boxes[$i]);    
        //echo $boxes[$i];      
    }       

    //Adding data to DB
    $this->Osoby_m->nowa_osoba($dane);  

    }   

    /////////////////////////////////////////
    ////// PROCESSING CODE END //////////////
    ////////////////////////////////////////

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('formularz', $data);
    }
    else
    {
        $this->load->view('wyslano_formularz');
    }
}

My checkboxes in View:

  <?php foreach ($szkolenia as $szkolenie): ?>
  <div class="checkbox">
        <label><input id="szkolenie<?php echo $szkolenie->id; ?>" type="checkbox" name="formChecks[]" value="<?php echo set_value('formChecks[]'); ?>"> <?php echo $szkolenie->nazwa_szkolenia; ?></label>
        <br>
    </div>
    <?php endforeach; ?>

After submiting this form with blank required input script doesnt reload checkboxes from database and display an error. This is happening event, when I put $data['szkolenia'] = $this->Szkolenie_m->pobierz(); into

if ($this->form_validation->run() == FALSE) { $this->load->view('formularz', $data); }

This is error I get:

enter image description here

  • 写回答

1条回答 默认 最新

  • dongqian5569 2015-12-10 10:30
    关注

    If you want to load checkboxes on validation error,

    use set_checkbox('name', 'value');

    Like this,

    set_checkbox('formChecks', 'value');
    

    Getting error because, you are trying to echo an array.

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的