我在CodeIgniter视图中有表单,其中复选框是从数据库生成的。
当我在不勾选任何复选框的情况下提交表单时,我收到错误:
消息:未定义的索引:formChecks
我的控制器:
public function formularz2()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//Names of checkboxes generated from model (DB)
$data['szkolenia'] = $this->Szkolenie_m->pobierz();
//Validation
$this->form_validation->set_rules('imie', 'Imię', 'required');
$this->form_validation->set_rules('accept_terms_checkbox', 'Checkbox', 'required');
$this->form_validation->set_message('required', 'Błąd: Powyższe pole jest wymagane');
//SUBMMIT
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,
);
//Adding checked boxes to another database
$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]);
}
$this->Osoby_m->nowa_osoba($dane);
}
if ($this->form_validation->run() == FALSE)
{
$this->load->view('formularz', $data);
}
else
{
$this->load->view('wyslano_formularz');
}
}
我的观点:
<?php echo form_open(); ?>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10" style="color: #c92e39;">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php echo set_value('email'); ?>">
<p class="help-block"><?php echo form_error('email'); ?></p>
</div>
</div>
<div class="form-group">
<label for="imie" class="col-sm-2 control-label">Imię:</label>
<div class="col-sm-10" style="color: #c92e39;">
<input type="text" class="form-control" id="imie" name="imie" placeholder="Imię" value="<?php echo set_value('imie'); ?>">
<p class="help-block"><?php echo form_error('imie'); ?></p>
</div>
</div>
<div class="form-group">
<label for="nazwisko" class="col-sm-2 control-label">Nazwisko:</label>
<div class="col-sm-10" style="color: #c92e39;">
<input type="text" class="form-control" id="nazwisko" name="nazwisko" placeholder="Nazwisko" value="<?php echo set_value('nazwisko'); ?>">
<p class="help-block"><?php echo form_error('nazwisko'); ?></p>
</div>
</div>
<div class="form-group">
<label for="firma" class="col-sm-2 control-label">Firma:</label>
<div class="col-sm-10" style="color: #c92e39;">
<input type="text" class="form-control" id="firma" name="firma" placeholder="Firma" value="<?php echo set_value('firma'); ?>">
<p class="help-block" style="color:red;"><?php echo form_error('firma'); ?></p>
</div>
</div>
<div class="col-sm-12" style="margin-bottom: 30px; margin-top: 30px;"><h3>Wybierz interesujące Cię szkolenia</h3></div>
<div class="form-group">
<label class="col-sm-2 control-label">Szkolenia:</label>
<div class="col-sm-10">
////////////////////////
///MY CHECKBOXES LOOP///
////////////////////////
<?php foreach ($szkolenia as $szkolenie): ?>
<div class="checkbox">
<label><input id="szkolenie<?php echo $szkolenie->id; ?>" type="checkbox" name="formChecks[]" value="<?php echo $szkolenie->id; ?>"> <?php echo $szkolenie->nazwa_szkolenia; ?></label>
<br>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-12" style="margin-top: 50px; ">
<input type="checkbox" name="accept_terms_checkbox" value="1"/> Zgadzam się na otrzymywanie maili od firmy Gamma<br>
<span style="color: #c92e39;"><p><?php echo form_error('accept_terms_checkbox') ?></p></span>
</div>
</div>
<button type="submit" class="btn btn-lg btn-success" style="margin-top: 50px;">Odbierz Voucher</button>
<?php echo form_close(); ?>
我如何修复此错误,或者对数据库formCheck []的复选框进行验证?
答案 0 :(得分:1)
此行显示错误:
$ boxes = $ _POST [&#39; formChecks&#39;];
只需检查一下:
$ boxes = empty($ _ POST [&#39; formChecks&#39;])? array():( array)$ _ POST [&#39; formChecks&#39;];
答案 1 :(得分:0)
尝试替换它,
$boxes = $_POST['formChecks'];
与
$boxes = $this->input->post('formChecks');
然后检查条件
if(count($boxes)){
// checked(may be few checkboxes)
} else {
// not checked
}