我创建一个页面,用户可以从表中搜索数据(表格数据),然后将搜索结果输入另一个表格(表格XY)。我想要实现的是当搜索结果出现时,它出现在另一个表单中,并且当提交第二个表单时,数据将插入到表XY中。但到目前为止,我所做的只能实现第一个目标,即从表格数据中搜索数据。当验证运行False时,错误似乎完成表单,但是当验证运行为True时,我稍后检查时数据不在表XY中。没有错误通知。我不知道我哪里做错了。这是我的代码: Controller Data.php:
public function index(){
$this->load->view('form');
}
public function search_xy(){
$this->form_validation->set_rules('id', 'ID', 'required|numeric|exact_length[16]');
if($this->form_validation->run() == false) {
$this->index();
} else {
$this->m_data->search_xy();
$this->index();
}
}
public function insert_xy(){
$this->form_validation->set_rules('id', 'ID', 'required');
$this->form_validation->set_rules('xname', 'X Name', 'required|alphanumeric');
$this->form_validation->set_rules('yname', 'Y Name', 'required|alphanumeric');
$this->form_validation->set_rules('xage', 'X Age', 'required');
$this->form_validation->set_rules('yage', 'Y Age', 'required');
$this->form_validation->set_rules('children', 'Children', 'required|numeric');
if($this->form_validation->run() == false) {
$this->index();
} else {
$this->m_xy->insert_xy();
$this->session->set_flashdata('message', '<div class="alert alert-success" data-dismiss="alert">Insert Data to XY Table Success!</div>');
$this->index();
}
}
模型m_data.php:
public function search_pus(){
$id = $this->input->post('id');
$this->db->empty_table('searched', TRUE);
$this->db->query("INSERT INTO 'searched' SELECT a.id, a.name as xname, a.age as xage, b.name as yname, b.age as yage FROM searched a, searched b WHERE a.gender = 1 AND b.gender = 2 AND a.id = '$id' AND b.id = '$id'");
return $this->db->query("SELECT * FROM searched")->result_array();
}
模型m_xy.php:
public function insert_xy(){
$data = array(
'id' => $this->input->post('id'),
'xname' => $this->input->post('xname'),
'yname' => $this->input->post('yname'),
'xage' => $this->input->post('xage'),
'yage' => $this->input->post('yage'),
'children' => $this->input->post('children'));
$this->db->insert('XY', $data);
}
查看form.php:
<?php $attributes = array("class" => "form-inline"); echo form_open('data/search_xy', $attributes);?>
<fieldset>
<div class="form-group">
<label class="control-label" for="id">Enter ID :</label>
<input type="text" name="id" class="form-control" id="id" value="<?php echo set_value('id'); ?>" />
<button class="btn btn-info btn-md" type="submit"> Search </button><span class="text-danger"><?php echo form_error('id');?></span>
</div>
</fieldset>
<?php echo form_close(); ?>
<?php echo form_open('data/insert_xy'); ?>
<fieldset>
<div class="form-group">
<h4 class="text-left text-primary">Search Result:</h4>
<table style="font-size:11px;" class="table table-condense table-bordered table-hover display" cellspacing="0" width="100%">
<thead>
<tr class="primary">
<th></th>
<th>X Name</th>
<th>Y Name</th>
<th>X age</th>
<th>Y age</th>
</tr>
</thead>
<tbody>
<?php foreach($search_result as $row): ?>
<tr>
<td><input type="checkbox" class="form-control" name="no_kk" id="no_kk" value="<?php echo $row['no_kk'];?>" /></td>
<td><input class="form-control" name="xname" value="<?php echo $row['xname'];?>" /></td>
<td><input class="form-control" name="yname" value="<?php echo $row['yname'];?>" /></td>
<td><input class="form-control" name="xage" value="<?php echo $row['xage'];?>" /></td>
<td><input class="form-control" name="yage" value="<?php echo $row['yage'];?>" /></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<div class="col-md-3">
<select class="form-control" name="children" value="<?php echo set_value('children');?>">
<option value="">-- How Many Children? --</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value=">3">3 or more</option>
</select>
<span class="text-danger"><?php echo form_error('children'); ?></span>
</div>
<div class="col-md-3">
<input class="btn btn-primary btn-md" type="submit" value="Save" />
</div>
</div>
</fieldset>
<?php echo form_close() ?>
当我点击“保存到XY”时,我希望将我检查的值插入表XY ..
是因为表格在表格中吗?
答案 0 :(得分:0)
试试这个......
控制器: -
公共职能指数(){
$data = array();
$id = $this->input->get('id');
if($id!=''){
$data = $this->m_data->search_xy();
}
$this->load->view('form',$data); }
public function insert_xy(){
$this->form_validation->set_rules('id', 'ID', 'required');
$this->form_validation->set_rules('xname', 'X Name', 'required|alphanumeric');
$this->form_validation->set_rules('yname', 'Y Name', 'required|alphanumeric');
$this->form_validation->set_rules('xage', 'X Age', 'required');
$this->form_validation->set_rules('yage', 'Y Age', 'required');
$this->form_validation->set_rules('children', 'Children', 'required|numeric');
if($this->form_validation->run() == false) {
redirect('data');
} else {
$this->m_xy->insert_xy();
$this->session->set_flashdata('message', '<div class="alert alert-success" data-dismiss="alert">Insert Data to XY Table Success!</div>');
redirect('data');
} }
查看: -
更改echo form_open('data',$ attributes);搜索表单