dongli5785 2015-09-18 08:32
浏览 34

Codeigniter表格结果作为另一种表格输入值。 第二个表格没有做它应该做的事情

I make a page where a user can search a data from a table(table Data) to then input the search result into another table(table XY). What I am trying to achieve is when the search result appear, it appears inside another form, and when the second form is submitted, the data is inserted to table XY. But so far what I made can only achieve the first goal, which is searching data from table Data. When validations run False the errors appear to complete the form, but when the validations run True, the data is not in the table XY when I checked it later. No error notifications. I don't know where do I do wrong. Here is my code: 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();
  }
}

Model 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();
}

Model 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);
}

View 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() ?>

This the view printscreen incase you don't get what I meant: When I click "Save to XY" I wish for the value I checked be inserted in table XY

When I click "Save to XY" I wish for the value I checked be inserted in table XY..

Is it because the form is in a table?

  • 写回答

1条回答 默认 最新

  • drblhw5731 2015-09-18 11:35
    关注

    Try this...

    Controller :-

    public function index(){

    $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');
    } }
    

    View :-

    Change echo form_open('data', $attributes); for search form

    评论

报告相同问题?

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析