验证codeigniter后下拉列表中的选定值未出现
Selected value in dropdown not appearing after validation codeigniter
我有 select-box 和一些文本框。在验证后提交的表单上,我如何才能将之前 selected 的值保留到 select 框中。?我正在使用代码 Igniter 验证类。所有其他验证都有效
我是 CodeIgniter 的新手,请帮助我。
这是我的代码
管理员:
<?php
class Bank_branch extends CI_Controller {
public function __construct(){
parent::__construct();
$data['row'] = $this->session->userdata('logged_in');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div id="errorDiv">', '</div>');
$this->load->model('bank_branch_model');
$this->load->model('banks_model');
}
function index()
{
if($this->session->userdata('logged_in'))
{
$data['row'] = $this->session->userdata('logged_in');
$this->load->view('header',$data);
$data['result'] = $this->bank_branch_model->all_bank_branches();
$this->load->view('settings_sidebar');
$this->load->view('bank_branch_view',$data);
$this->load->view('footer');
}
else
{
//If no session, redirect to login page
$this->load->helper('url');
redirect('login/','refresh');
}
}
function bank_branch_form()
{
$data['row'] = $this->session->userdata('logged_in');
$this->load->view('header',$data);
$data['banks'] = $this->banks_model->all_banks();
$this->load->view('settings_sidebar');
$this->load->view('bank_branch_add',$data);
$this->load->view('footer');
}
function bank_branch_insert()
{
$data['title'] = 'Bank Branch Insertion Form';
$bank_name = $this->input->post('sel_bank_name');
$this->form_validation->set_rules('sel_bank_name', 'Bank Name', 'required|callback_select_validate');
$this->form_validation->set_rules('txt_place_details', 'Place Details', 'trim|required|xss_clean');
$this->form_validation->set_rules('txt_ifsc_code', 'IFSC Code', 'trim|required|xss_clean');
$this->form_validation->set_rules('txt_contact_no1', 'Contact No 1', 'trim|xss_clean|min_length[10]|max_length[15]');
$this->form_validation->set_rules('txt_contact_no2', 'Contact No 2', 'trim|xss_clean|min_length[10]|max_length[15]');
$this->form_validation->set_rules('txt_email', 'Email', 'trim|required|xss_clean|valid_email');
if($this->form_validation->run() === FALSE)
{
$data['row'] = $this->session->userdata('logged_in');
$this->load->view('header',$data);
$data['banks'] = $this->banks_model->all_banks();
$data['result'] = $this->bank_branch_model->all_bank_branches();
$this->load->view('settings_sidebar');
$this->load->view('bank_branch_add',$data);
$this->load->view('footer');
}
else
{
$this->bank_branch_model->insert_bank_branch();
redirect('bank_branch', 'refresh');
}
}
function select_validate($bank_name)
{
if($bank_name=="none")
{$this->form_validation->set_message('select_validate', 'Please Select Your Branch.');
return false;}
else
{ return true; }
}
}
?>
查看:
<div class="main_content">
<div class="table_top">
<span class="table_title">Add Bank Branch</span>
<span class="add_record_btn">
<a href="javascript:history.back()" title="Close"><img src="<?php echo base_url(); ?>application/assets/img/icons/back-button.png" /></a>
</span>
</div>
<?php echo form_open('bank_branch/bank_branch_insert'); ?>
<table width="769" align="center">
<tr>
<td width="191" height="41">Bank</td>
<td width="241">
<select class="armr_text_box" id="sel_bank_name" name="sel_bank_name" >
<option value="none">-Select-</option>
<?php
foreach($banks as $bank):
?>
<option value="<?php echo $bank['bank_id']; ?>"><?php echo $bank['bank_name']; ?></option>
<?php
endforeach;
?>
</select>
</td>
<td width="321"><?php echo form_error('sel_bank_name'); ?></td>
</tr>
<tr>
<td height="56">Place Details</td>
<td><textarea class="armr_text_area" id="txt_place_details" name="txt_place_details" onkeyup="ucfirst(this)"><?php echo set_value('txt_place_details'); ?></textarea>
</td>
<td> <?php echo form_error('txt_place_details'); ?></td>
</tr>
<tr>
<td height="43">IFSC Code</td>
<td><input type="text" class="armr_text_box" id="txt_ifsc_code" name="txt_ifsc_code" value="<?php echo set_value('txt_ifsc_code'); ?>"/>
</td>
<td><?php echo form_error('txt_ifsc_code'); ?></td>
</tr>
<tr>
<td height="41">Contact No 1</td>
<td><input type="text" class="armr_text_box" id="txt_contact_no1" name="txt_contact_no1" value="<?php echo set_value('txt_contact_no1'); ?>" />
</td>
<td><?php echo form_error('txt_contact_no1'); ?></td>
</tr>
<tr>
<td height="45">Contact No 2</td>
<td><input type="text" class="armr_text_box" id="txt_contact_no2" name="txt_contact_no2" value="<?php echo set_value('txt_contact_no2'); ?>"/>
</td>
<td><?php echo form_error('txt_contact_no2'); ?></td>
</tr>
<tr>
<td height="44">Email</td>
<td><input type="text" class="armr_text_box" id="txt_email" name="txt_email" value="<?php echo set_value('txt_email'); ?>"/>
</td>
<td> <?php echo form_error('txt_email'); ?></td>
</tr>
<tr>
<td height="43"></td>
<td><input type="submit" value="Submit" class="armr_submit_btn" />
<input type="reset" value="Cancel" class="armr_cancel_btn" /></td>
<td> </td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
</div>
试试这个:
<select class="armr_text_box" id="sel_bank_name" name="sel_bank_name" >
<option value="none">-Select-</option>
<?php
$sel_bank_name = set_value('sel_bank_name');
foreach($banks as $bank):
?>
<option value="<?php echo $bank['bank_id']; ?>" <?php $sel_bank_name == $bank['bank_id'] ? "selected":"" ?>><?php echo $bank['bank_name']; ?></option>
<?php
endforeach;
?>
</select>
在 CI 中使用 set_select()
辅助函数。
Link 是 Here
例如:
<select class="armr_text_box" id="sel_bank_name" name="sel_bank_name" >
<option value="none">-Select-</option>
<?php
foreach($banks as $bank):
?>
<option value="<?php echo $bank['bank_id']; ?>" <?php echo set_select('sel_bank_name', $bank['bank_id']); ?>><?php echo $bank['bank_name']; ?></option>
<?php
endforeach;
?>
</select>//the ser
试试这个方法
$BankList['none'] = '-Select-';
foreach($banks as $bank):
$BankList[$bank['bank_id']] = $bank['bank_name'];
endforeach;
echo form_dropdown('sel_bank_name',$BankList,set_value('sel_bank_name')," class='armr_text_box' id='sel_bank_name' ");
我有 select-box 和一些文本框。在验证后提交的表单上,我如何才能将之前 selected 的值保留到 select 框中。?我正在使用代码 Igniter 验证类。所有其他验证都有效 我是 CodeIgniter 的新手,请帮助我。
这是我的代码
管理员:
<?php
class Bank_branch extends CI_Controller {
public function __construct(){
parent::__construct();
$data['row'] = $this->session->userdata('logged_in');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div id="errorDiv">', '</div>');
$this->load->model('bank_branch_model');
$this->load->model('banks_model');
}
function index()
{
if($this->session->userdata('logged_in'))
{
$data['row'] = $this->session->userdata('logged_in');
$this->load->view('header',$data);
$data['result'] = $this->bank_branch_model->all_bank_branches();
$this->load->view('settings_sidebar');
$this->load->view('bank_branch_view',$data);
$this->load->view('footer');
}
else
{
//If no session, redirect to login page
$this->load->helper('url');
redirect('login/','refresh');
}
}
function bank_branch_form()
{
$data['row'] = $this->session->userdata('logged_in');
$this->load->view('header',$data);
$data['banks'] = $this->banks_model->all_banks();
$this->load->view('settings_sidebar');
$this->load->view('bank_branch_add',$data);
$this->load->view('footer');
}
function bank_branch_insert()
{
$data['title'] = 'Bank Branch Insertion Form';
$bank_name = $this->input->post('sel_bank_name');
$this->form_validation->set_rules('sel_bank_name', 'Bank Name', 'required|callback_select_validate');
$this->form_validation->set_rules('txt_place_details', 'Place Details', 'trim|required|xss_clean');
$this->form_validation->set_rules('txt_ifsc_code', 'IFSC Code', 'trim|required|xss_clean');
$this->form_validation->set_rules('txt_contact_no1', 'Contact No 1', 'trim|xss_clean|min_length[10]|max_length[15]');
$this->form_validation->set_rules('txt_contact_no2', 'Contact No 2', 'trim|xss_clean|min_length[10]|max_length[15]');
$this->form_validation->set_rules('txt_email', 'Email', 'trim|required|xss_clean|valid_email');
if($this->form_validation->run() === FALSE)
{
$data['row'] = $this->session->userdata('logged_in');
$this->load->view('header',$data);
$data['banks'] = $this->banks_model->all_banks();
$data['result'] = $this->bank_branch_model->all_bank_branches();
$this->load->view('settings_sidebar');
$this->load->view('bank_branch_add',$data);
$this->load->view('footer');
}
else
{
$this->bank_branch_model->insert_bank_branch();
redirect('bank_branch', 'refresh');
}
}
function select_validate($bank_name)
{
if($bank_name=="none")
{$this->form_validation->set_message('select_validate', 'Please Select Your Branch.');
return false;}
else
{ return true; }
}
}
?>
查看:
<div class="main_content">
<div class="table_top">
<span class="table_title">Add Bank Branch</span>
<span class="add_record_btn">
<a href="javascript:history.back()" title="Close"><img src="<?php echo base_url(); ?>application/assets/img/icons/back-button.png" /></a>
</span>
</div>
<?php echo form_open('bank_branch/bank_branch_insert'); ?>
<table width="769" align="center">
<tr>
<td width="191" height="41">Bank</td>
<td width="241">
<select class="armr_text_box" id="sel_bank_name" name="sel_bank_name" >
<option value="none">-Select-</option>
<?php
foreach($banks as $bank):
?>
<option value="<?php echo $bank['bank_id']; ?>"><?php echo $bank['bank_name']; ?></option>
<?php
endforeach;
?>
</select>
</td>
<td width="321"><?php echo form_error('sel_bank_name'); ?></td>
</tr>
<tr>
<td height="56">Place Details</td>
<td><textarea class="armr_text_area" id="txt_place_details" name="txt_place_details" onkeyup="ucfirst(this)"><?php echo set_value('txt_place_details'); ?></textarea>
</td>
<td> <?php echo form_error('txt_place_details'); ?></td>
</tr>
<tr>
<td height="43">IFSC Code</td>
<td><input type="text" class="armr_text_box" id="txt_ifsc_code" name="txt_ifsc_code" value="<?php echo set_value('txt_ifsc_code'); ?>"/>
</td>
<td><?php echo form_error('txt_ifsc_code'); ?></td>
</tr>
<tr>
<td height="41">Contact No 1</td>
<td><input type="text" class="armr_text_box" id="txt_contact_no1" name="txt_contact_no1" value="<?php echo set_value('txt_contact_no1'); ?>" />
</td>
<td><?php echo form_error('txt_contact_no1'); ?></td>
</tr>
<tr>
<td height="45">Contact No 2</td>
<td><input type="text" class="armr_text_box" id="txt_contact_no2" name="txt_contact_no2" value="<?php echo set_value('txt_contact_no2'); ?>"/>
</td>
<td><?php echo form_error('txt_contact_no2'); ?></td>
</tr>
<tr>
<td height="44">Email</td>
<td><input type="text" class="armr_text_box" id="txt_email" name="txt_email" value="<?php echo set_value('txt_email'); ?>"/>
</td>
<td> <?php echo form_error('txt_email'); ?></td>
</tr>
<tr>
<td height="43"></td>
<td><input type="submit" value="Submit" class="armr_submit_btn" />
<input type="reset" value="Cancel" class="armr_cancel_btn" /></td>
<td> </td>
</tr>
</table>
<?php echo form_close(); ?>
</div>
</div>
试试这个:
<select class="armr_text_box" id="sel_bank_name" name="sel_bank_name" >
<option value="none">-Select-</option>
<?php
$sel_bank_name = set_value('sel_bank_name');
foreach($banks as $bank):
?>
<option value="<?php echo $bank['bank_id']; ?>" <?php $sel_bank_name == $bank['bank_id'] ? "selected":"" ?>><?php echo $bank['bank_name']; ?></option>
<?php
endforeach;
?>
</select>
在 CI 中使用 set_select()
辅助函数。
Link 是 Here
例如:
<select class="armr_text_box" id="sel_bank_name" name="sel_bank_name" >
<option value="none">-Select-</option>
<?php
foreach($banks as $bank):
?>
<option value="<?php echo $bank['bank_id']; ?>" <?php echo set_select('sel_bank_name', $bank['bank_id']); ?>><?php echo $bank['bank_name']; ?></option>
<?php
endforeach;
?>
</select>//the ser
试试这个方法
$BankList['none'] = '-Select-';
foreach($banks as $bank):
$BankList[$bank['bank_id']] = $bank['bank_name'];
endforeach;
echo form_dropdown('sel_bank_name',$BankList,set_value('sel_bank_name')," class='armr_text_box' id='sel_bank_name' ");