Reputation: 4003
I am building a site where the user can become a member and have a minimal profile. Nevertheless, in the case that this user wishes to update his records (About Me, Email, Phone Number,etc), I want to have a link "Edit Profile". Once clicked a form is loaded with input fields with values populated from database record of that user. He/she can change the values in the fields and click Save Changes, then those are committed to database.
I tried doing it several times but kept failing. Below is the code I tried to load data in form:
public function load_edit()
{
//check user logged in
if(($this->session->userdata('username')!=""))
{
$data;
$result = $this->profileModel->load_user_editable_data($this->session->userdata('username'));
//check user data loaded successfully
if(isset($result))
{
//get the user information and store to $data array
foreach($result as &$value)
{
$data = $value;
}
$this->load->view('profile_edit', $data);
}
}else{
$this->login();
}
}
And to update record in database table:
public function update_edit()
{
$this->form_validation->set_rules('fullname', 'الاسم الكامل', 'isset|required|alpha_dash');
if(isset($_POST))
{
//check user logged in
if(($this->session->userdata('username')!=""))
{
//check that there are no form validation errors
if($this->form_validation->run() == FALSE)
{
$data = $this->profileModel->load_user_editable_data($this->session->userdata('username'));
$this->load->view('profile_edit', $data);
}else{
$result = $this->profileModel->update_profile($this->session->userdata('username'));
if($result){
$this->load->view('profile_edit', $result);
}
}
}
}else{
$this->load->view('error');
}
}
The main problem I faced with the execution of above code, is that somehow when I execute update_edit, it always tells me that form validation failed and even though the condition of the field is met.
Thanks for help in advance :)
Upvotes: 1
Views: 648
Reputation: 4079
You can do it with one controller function and more easy:
public function edit($user_id = null) {
$data['url'] = "edit/$user_id"; // URL
$this->load->vars($data); // Load URL for FORM
$user = $this->profileModel->get_user($user_id); // Select USER by ID
if($this->input->post('save')) {
$this->form_validation->set_rules('username', 'Username', 'required');
if($this->form_validation->run()) {
$user['username'] = $this->input->post('username'); // Get new username
$this->profileModel->edit($user); // Edit
// redirect somewhere. Edit done!
}
}
$this->load->view('content', 'profile_edit', $user);
}
Here is the simple Example. You can do it with Session ID, or ID with parameter (like I did).
I hope that you will understand
Upvotes: 0
Reputation: 1591
I'm not aware of validation rule isset. Try without it.
If then the problem persists, try doing print_r($_POST) in update_edit, before you check if isset($_POST). Also check if your Session variable username is present.
Upvotes: 1