本文介绍了只有“Type = Text” & “Textarea”重定向错误后重新填充;但是“选择” & “复选框”不在CodeIgniter 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新的CI,并试图在这种形式,如果验证输出错误,那么用户不应该松动字段值。但它只是重新填充文本&

 < span class =error><?php echo $ validation_errors; ?>< / span> 
<?php echo form_open_multipart('user / post_property'); >
< input name =v_item_titleplaceholder =Property Title Goes Here ..value =<?php echo $ v_item_title;?& />
< textarea name =v_item_descriptionplaceholder =Description><?php echo $ v_item_description; ?>< / textarea>
< select name =v_item_categoryvalue =<??php echo $ v_item_category;?>>
< option value =>选择< / option>
< option value =Apartment<?php echo $ v_item_category; ?>>公寓< / option>
< option value =Barn<?php echo $ v_item_category; ?>> Barn< / option>
< / option>
< / select>
< input type =hiddenname =v_item_payment_visavalue =0/>
< input type =checkboxname =v_item_payment_visavalue =1<?php echo $ v_item_payment_visa; > />< span class =li-font> VISA< / span>
< input type =submitvalue =Submit>
<?php echo form_close(); >

控制器 -

 code> $ this-> form_validation-> set_rules('v_item_title','Property title','trim | required | xss_clean | max_length [100]'); 
if($ this-> form_validation-> run()== FALSE){
$ this-> session-> set_userdata('validation_errors',validation_errors());
$ this-> session-> mark_as_flash('validation_errors'); // data will automatically delete their after redirect
$ this-> session-> set_flashdata('v_item_title',$ this-> input-> post('v_item_title'));
$ this-> session-> flashdata('v_item_title');
$ this-> session-> set_flashdata('v_item_description',$ this-> input-> post('v_item_description'))
$ this-> session-> flashdata('v_item_description');
$ this-> session-> set_flashdata('v_item_category',$ this-> input-> post('v_item_category'));
$ this-> session-> flashdata('v_item_category');
$ this-> session-> set_flashdata('v_item_payment_visa',$ this-> input-> post('v_item_payment_visa'));
$ this-> session-> flashdata('v_item_payment_visa');
redirect('user / dashboard#new');
} else {

重新导向至 -

  public function dashboard(){
if($ this-> session-> userdata('is_logged_in')){
$ data [ validation_errors'] = $ this-> session-> userdata('validation_errors');
$ data ['v_item_title'] = $ this-> session-> userdata('v_item_title');
$ data ['v_item_description'] = $ this-> session-> userdata('v_item_description');
$ data ['v_item_category'] = $ this-> session-> userdata('v_item_category');
$ data ['v_item_payment_visa'] = $ this-> session-> userdata('v_item_payment_visa');
$ data ['homepage'] ='../../templates/vacations/users/dashboard';
$ this-> load-> view('template_users',$ data);
} else {

花了一些时间,寻求帮助。

解决方案

最后我得到了工作 -

 < span class =error><?php echo $ validation_errors; ?>< / span> 
<?php echo form_open_multipart('user / post_property'); >
< input name =v_item_titleplaceholder =Property Title Goes Here ..value =<?php echo $ v_item_title;?& />
< textarea name =v_item_descriptionplaceholder =Description><?php echo $ v_item_description; ?>< / textarea>
< select name =v_item_category>
< option value =0>选择< / option>
< option value =Apartment<?php if($ this-> session-> flashdata('v_item_category')==='Apartment')echo('selected')?> ;公寓< / option>
< option value =Barn<?php if($ this-> session-> flashdata('v_item_category')==='Barn')echo('selected')?> ; Barn< / option>
< / option>
< / select>
< input type =hiddenname =v_item_payment_visavalue =0/>
< input type =checkboxname =v_item_payment_visavalue =1<?php if($ this-> session-> flashdata('v_item_payment_visa'))echo'checked = '; > />< span class =li-font> VISA< / span>
< input type =submitvalue =Submit>
<?php echo form_close(); >


Hi I am new with CI and trying to work on this form where if validation outputs error then user shouldn't loose the field values. But it only re-populates text & textarea, not checkboxes and select.

Form-

<span class="error"><?php echo $validation_errors; ?></span>
<?php echo form_open_multipart('user/post_property'); ?>
<input name="v_item_title" placeholder="Property Title Goes Here.." value="<?php echo $v_item_title; ?>" />
<textarea name="v_item_description" placeholder="Description" ><?php echo $v_item_description; ?></textarea>
<select name="v_item_category" value="<?php echo $v_item_category; ?>">
<option value="">Select</option>
<option value="Apartment" <?php echo $v_item_category; ?>>Apartment</option>
<option value="Barn" <?php echo $v_item_category; ?>>Barn</option>
</option>
</select>
<input type="hidden" name="v_item_payment_visa" value="0"  />
<input type="checkbox" name="v_item_payment_visa" value="1" <?php echo $v_item_payment_visa; ?> /><span class="li-font">VISA</span>
<input type="submit" value="Submit">
<?php echo form_close(); ?>

Controller-

$this->form_validation->set_rules('v_item_title', 'Property title', 'trim|required|xss_clean|max_length[100]');
if($this->form_validation->run() == FALSE) {
$this->session->set_userdata('validation_errors', validation_errors());
$this->session->mark_as_flash('validation_errors'); // data will automatically delete themselves after redirect
$this->session->set_flashdata('v_item_title', $this->input->post('v_item_title'));
$this->session->flashdata('v_item_title');
$this->session->set_flashdata('v_item_description', $this->input->post('v_item_description'));
$this->session->flashdata('v_item_description');
$this->session->set_flashdata('v_item_category', $this->input->post('v_item_category'));
$this->session->flashdata('v_item_category');
$this->session->set_flashdata('v_item_payment_visa', $this->input->post('v_item_payment_visa'));
$this->session->flashdata('v_item_payment_visa');
redirect('user/dashboard#new');
} else {

Redirects to-

public function dashboard() {
if($this->session->userdata('is_logged_in')){
$data['validation_errors'] = $this->session->userdata('validation_errors');
$data['v_item_title'] = $this->session->userdata('v_item_title');
$data['v_item_description'] = $this->session->userdata('v_item_description');
$data['v_item_category'] = $this->session->userdata('v_item_category');
$data['v_item_payment_visa'] = $this->session->userdata('v_item_payment_visa');
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
}else{

Had spent some good time on it but getting nowhere. Seeking help here.

解决方案

Finally I got it working-

<span class="error"><?php echo $validation_errors; ?></span>
<?php echo form_open_multipart('user/post_property'); ?>
<input name="v_item_title" placeholder="Property Title Goes Here.." value="<?php echo $v_item_title; ?>" />
<textarea name="v_item_description" placeholder="Description" ><?php echo $v_item_description; ?></textarea>
<select name="v_item_category">
<option value="0">Select</option>
<option value="Apartment" <?php if($this->session->flashdata('v_item_category') === 'Apartment') echo('selected')?>>Apartment</option>
<option value="Barn" <?php if($this->session->flashdata('v_item_category') === 'Barn') echo('selected')?>>Barn</option>
</option>
</select>
<input type="hidden" name="v_item_payment_visa" value="0"  />
<input type="checkbox" name="v_item_payment_visa" value="1" <?php if($this->session->flashdata('v_item_payment_visa')) echo 'checked="checked"'; ?> /><span class="li-font">VISA</span>
<input type="submit" value="Submit">
<?php echo form_close(); ?>

这篇关于只有“Type = Text” &amp; “Textarea”重定向错误后重新填充;但是“选择” &amp; “复选框”不在CodeIgniter 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 10:25