2011-12-02 20 views
1

我通過URL傳遞變量$id,並以表單形式更新加載數據,但是當我要驗證表單時,無法識別值$id驗證CodeIgniter中的表單後未設置參數

function myfunction() { 
    $id = $this->uri->segment(4); 
    echo $id; //yes, print the value 

    $data_user = $this->admin_model->query_data_user($id); 
    $direccion = $data_user[0]->address; 
    $phone = $data_user[0]->phone; 

    $this->data['address'] = array(
      'name' => 'address', 
      'id' => 'address', 
      'value' => $address, 
      'class' => 'input', 
    ); 

    $this->data['phone'] = array(
      'name' => 'phone', 
      'id' => 'phone', 
      'value' => $phone, 
      'class' => 'input', 
    ); 

    echo $id; //yes, print the value 

    $this->form_validation->set_rules('address', 'Address', 'xss_clean|max_length[100]'); 
    $this->form_validation->set_rules('phone', 'Phone', 'required|xss_clean|max_length[20]|is_natural_no_zero'); 

    if ($this->form_validation->run() == true) { 
     echo $id; //here NOT PRINT $id 
     $data = array(
       'address' => $this->input->post('address'), 
       'phone' => $this->input->post('phone'), 
     ); 

     $id = $this->uri->segment(4); 
     echo $id; //here not print the value $id 

     $this->ion_auth->update_user($id, $data); 
     $this->load->view('includes/template_mensajes', $data); 
    } 
    $this->load->view('users/update_user', $this->data); 
} 

價值$id時,它驗證了我的形式,什麼是我的錯誤是不承認?

+0

您是否嘗試使變量$ id爲全局? –

回答

5

你在哪裏提交你的表格。你確定你在窗體的action屬性的URI中傳遞了ID嗎?

例如您的形式打開的標籤應該是這個樣子

<form action="/admin/myfunction/<?= $id ?>">

或者使用笨幫手

echo form_open('admin/myfunction/' . $id);

+0

嗨。感謝您的回答。你已經回答了我的問題。 – cabita

+0

我認爲這是答案的一部分,並且是唯一的方法來完成我在我的答案中解釋的內容。但更重要的是使用$ this-> uri-> segment(4);在他的代碼中是錯誤的,應該使用CI控制器操作的正確方法來完成。 – MikeMurko

0

您需要將表單中的$ id變量作爲隱藏字段傳遞並將其作爲POST值返回。

1

如果我沒有弄錯,我的功能是在你的控制器中的一個動作叫「... mycontroller」讓我們說。那麼你應該只是發送這個網址:http://localhost/mycontroller/myfunction/1234

並更新您的操作爲接受ID參數$ id。 CodeIgniter將負責其餘的部分。

function myfunction($id) { 
... 
} 
+0

我試着用你的答案:anchor('user/myfunction /'。用戶['id'],'update')和我的函數函數myfunction($ id)併產生這個錯誤:遇到一個PHP錯誤。嚴重性:警告。 消息:缺少用戶:: myfunction()的參數1。文件名:controllers/user.php另一個錯誤是:遇到PHP錯誤。嚴重性:通知。消息:未定義的變量:id。 文件名:controllers/user.php。我希望這個選項適合我。感謝您的回答。 – cabita

+0

但是,我可以通過基於你的答案的值,在($ this-> form_validation-> run()== true){不能識別值$ id; 。感謝您的回答。 – cabita

+0

您的回答很有用。謝謝。 – cabita

0

加入我的ID我的網址,如結束後www.whsconcepts.com/?id=100, 我在表單中將它回叫。例如:

form_open('the url I want' . $_Get ['id'])