簡體   English   中英

將CodeIgniter發布數據傳遞給函數

[英]Passing CodeIgniter post data to a function

基本上,我希望多部分表單的控制器具有一個功能,該功能將發布數據提供給其他功能,例如預覽和編輯。

該功能如下:

function get_post_data()
{
  $post_data_array = array();
  // declaration of all form field names
  $variable_array = array('form_field_1', 'form_field_2', ... 'form_field_n');

  for ($i = 0; $i < count($variable_array); $i++) {
    $variable_value = $this->input->post($variable_array[$i]);
    // turn them into an easy-to-use array
    $post_data_array[$variable_array[$i]] = $variable_value;
  }
  return $post_data_array;
}

這樣該函數將按以下方式訪問它:

function show_preview_form()
{
  $this->load->view('preview_form_view', $this->get_post_data() );
}

function send_to_database()
{
  $data_array = $this->get_post_data();
  $this->Model->insert_to_database($data_array['form_field_1'], ...);
}

當前它不起作用。 Firebug返回500 Internal Status Error狀態。 你們知道如何解決嗎?

我真的不想在每個需要它的函數中重復長的get_post_data

你為什么做這個?

您可以使用數組獲取帖子數據。

$post_data = $this->input->post();

...

http://ellislab.com/codeigniter/user-guide/libraries/input.html

您需要先為$post_data_array分配一個空數組,然后才能向其中添加值。

get_post_data函數的開頭添加$post_data_array = array();

更新:

get_post_data更改為_get_post_data可能也是一個好主意,這樣就不能直接從url訪問它。 http://ellislab.com/codeigniter/user-guide/general/controllers.html#private

是的,您可以簡單地使用

               $this->input->post(); 

獲取輸入字段的過帳數據。 此外,您可以通過set_rules檢查輸入的帖子字段為

                     $this->form_validation->set_rules('userName','Username','trim|regex_match[/^[a-z,0-9,A-Z_ ]{5,35}$/]|required|xss_clean');
    $this->form_validation->set_rules('userFirstName', 'First name','trim|regex_match[/^[a-z,0-9,A-Z_ ]{5,35}$/]|required|xss_clean');
    $this->form_validation->set_rules('userLastName', 'Last name','trim|regex_match[/^[a-z,0-9,A-Z_ ]{5,35}$/]|required|xss_clean');
    $this->form_validation->set_rules('userEmail', 'Email', 'trim|regex_match[/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/]|required|xss_clean');
    $this->form_validation->set_rules('userPass', 'Password', 'trim|regex_match[/^[a-z,0-9,A-Z]{5,35}$/]|required|xss_clean|md5|callback_check_database');
    if ($this->form_validation->run() == FALSE) {
        //your view to show if validation is false
    } else {
         $user_name = $this->input->post('userName');
        $userfname = $this->input->post('userFirstName');
        $userlname = $this->input->post('userLastName');
        $useremail = $this->input->post('userEmail');
        $userpass = $this->input->post('userPass');
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM