2016-05-14 88 views
1

我有一個像Codeigniter-如何保存表單值,如果誤差在提交

<input type="text" class="form-control" name="postal_code" id="form_control_1" value="<?php if($user->postal_code === NULL) { echo set_value('postal_code');} else { echo $user->postal_code;} ?>"> 
<label for="form_control_1">Postal Code</label> 
<span class="help-block">Postal Code is required</span> 

表單字段和我的功能是

public function postProfile() { 
    if ($_POST) { 


     $this->form_validation->set_rules('postal_code', 'Postal Code', 'required'); 
     $this->form_validation->set_rules('gender', 'Gender', 'required'); 
     $this->form_validation->set_rules('country', 'Country', 'required'); 
     $this->form_validation->set_rules('month', 'Month', 'required'); 
     $this->form_validation->set_rules('day', 'Day', 'required'); 
     $this->form_validation->set_rules('year', 'Year', 'required'); 
     $this->form_validation->set_rules('mobile_number', 'Mobile Number', 'required'); 


     if ($this->form_validation->run() === FALSE) { 

      //$this->session->set_flashdata('err', validation_errors()); 
      $this->session->set_flashdata('email_err', form_error('email')); 
      $this->session->set_flashdata('postal_code_err', form_error('postal_code')); 
      $this->session->set_flashdata('gender_err', form_error('gender')); 
      $this->session->set_flashdata('country_err', form_error('country')); 
      $this->session->set_flashdata('day_err', form_error('day')); 
      $this->session->set_flashdata('year_err', form_error('year')); 
      $this->session->set_flashdata('month_err', form_error('month')); 
      $this->session->set_flashdata('mobile_err', form_error('mobile_number')); 
      redirect('profile/' . $this->session->userdata['front']['id']); 
     } else { 

      $dob = $this->input->post('month') . '/' . $this->input->post('day') . '/' . $this->input->post('year'); 
      $userData = array(
       'email' => $this->input->post('email'), 
       'date_of_birth' => $dob, 
       'gender' => $this->input->post('gender'), 
       'country' => $this->input->post('country'), 
       'mobile_number' => $this->input->post('mobile_number'), 
       'postal_code' => $this->input->post('postal_code'), 
      ); 


      $updateUser = $this->user->updateUser($this->session->userdata['front']['id'], $userData); 
      if ($updateUser) { 
       $this->session->set_flashdata('err', '<div class="alert alert-success">Profile Updated Successfuly</div>'); 
       redirect('profile/' . $this->session->userdata['front']['id']); 
      } else { 
       echo "Un expected error "; 
       exit; 
      } 
     } 
    } else { 
     $this->logout(); 
    } 
} 

,但沒有顯示的情況下,郵政編碼字段值驗證錯誤

+0

給出問好SET_VALUE() - 將會大大降低所有的代碼HTTP://www.codeigniter。 com/user_guide/helpers/form_helper.html#set_value – cartalot

回答

0

當發生錯誤時,您需要渲染視圖而不是rediect()

if ($this->form_validation->run() == FALSE) { 
    // no need to set_flashdata here for form_error() 
    $this->load->view('view_form',$data); 
    return; 
}else{ 
    //success code 
} 

現在查看文件

<input type="text" class="form-control" name="postal_code" id="form_control_1" value="<?php echo set_value('postal_code');?>"> 
<?php echo form_error('postal_code','<p class="alert alert-danger">','</p>');?> 

現在,如果任何錯誤發生的形式將重新填充以前的數據。在同一時間,您將在您的postal_code字段下看到錯誤消息

+0

只需用'if($ this-> form_validation-> run()=== FALSE){'它應該只有兩個'=='而不是三個'=' ==' – user4419336

0

您可以在flashdata中設置這些值並將其重定向到您的表單。例如

if ($this->form_validation->run() == FALSE) { 

     $data = array(
       'errors' => validation_errors(), 
       'name' => $this->input->post('name')//check this 
      ); 

     $this->session->set_flashdata($data); 

     redirect('YOUR-URL-HERE'); 
    } 

而在表單輸入的表單使用價值屬性下面

<input type="text" class="form-control" name="name" placeholder="Name" value="<?= $this->session->flashdata('name'); ?>"> 
相關問題