2015-09-18 63 views
1

我製作了一個頁面,用戶可以從一個表格(表格數據)中搜索數據,然後將搜索結果輸入到另一個表格(表格XY)中。我試圖實現的是當搜索結果出現時,它出現在另一個表單中,並且當第二個表單被提交時,數據被插入到表XY中。但到目前爲止,我所做的只能實現第一個目標,即從表數據中搜索數據。當驗證運行False時,錯誤似乎完成了表單,但是當驗證運行爲True時,稍後檢查時數據不在XY表格中。沒有錯誤通知。我不知道我在哪裏做錯了。這裏是我的代碼: 控制器Data.php:Codeigniter Form結果作爲另一個表單輸入值。第二種形式沒有做它應該做的事

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

This view printsc穎櫃面你不明白我的意思: When I click "Save to XY" I wish for the value I checked be inserted in table XY

當我點擊「保存到XY」我希望爲我檢查表XY要插入的價值..

是不是因爲形式是一張桌子?

+0

我不確定,但在第二種形式中,您有一個循環,您可以在其中循環使用相同名稱的輸入字段。你應該使用''。當你想將它插入到PHP到DB,你必須遍歷你的輸入帖子,以獲得他們的價值觀。 – Iamzozo

+0

@Izozozo呃我不這麼想過。謝謝,我會在此搜索。 –

回答

0

嘗試......

控制器: -

公共功能指數(){

$data = array(); 

$id = $this->input->get('id'); 

if($id!=''){ 

    $data = $this->m_data->search_xy(); 

} 

$this->load->view('form',$data); } 

公共職能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);搜索表單

+0

對不起,遲到的迴應。其實我有一個問題,第二種形式(insert_xy),而不是第一種形式(search_xy)的輸入值。但通過你的代碼的外觀,我認爲你只是在索引函數中調用了一個搜索函數模型。所以它不會改變任何東西。不管怎麼說,還是要謝謝你 :)。 –

相關問題