Welcome Guest, Not a member yet? Register   Sign In
Database populated form fields (e.g. edit mode) and form validation
#3

[eluser]majidmx[/eluser]
As far as I guess, you're replacing data to the default one which is loaded from database.
if you use set_value method you can easily repopulate the data :

controller :
Code:
function edit_news($news_id)
{
    $this->load->model('Model_news');
    
    $current_news    =    $this->Model_news->get_news($news_id);
    
    $data['default']['title']        =    $current_news->nw_title;
    $data['default']['body']        =    $current_news->nw_body;
    
    $this->load->view('news_edit_view' , $data);        
}

function update_news()
{        
    $this->load->library('form_validation');    
    $this->form_validation->set_rules('title', 'News Title', 'xss_clean|trim|required');        
    $this->form_validation->set_rules('body', 'News Body', 'xss_clean|trim|required');    
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('news_edit_view');
    }else
    {
        // update the database
    }
}

and your view would look like this :
Code:
<input type="text" name="title" id="title" size="80" maxlength="255" value="<?PHP echo    set_value('title' , isset($default['title'])?$default['title']:'' );?>" />
<textarea name="body" id="body" rows="20" cols="60"><?PHP echo    set_value('body' , isset($default['body'])?$default['body']:'' );?></textarea>

note that, you don't need to fetch the record from database again in update_news

let me know if you can get it worked.
Cheers,
MajiD Fatemian


Messages In This Thread
Database populated form fields (e.g. edit mode) and form validation - by El Forum - 12-16-2008, 11:55 PM



Theme © iAndrew 2016 - Forum software by © MyBB