Welcome Guest, Not a member yet? Register   Sign In
Problem sending post data to model for insert/update
#1

[eluser]Nicholai[/eluser]
I've searched the forums for this and read the user guide, but can't find any examples that are basic enough for me to follow without getting lost. I am new to the MVC concept and am trying to learn it one piece at a time. My goal is to:
1. display a form in a view containing a hidden ID field and some text boxes
2. pre-populate the form with data from a table
3. allow the user to edit the text, and when clicking submit in the form, send the post data to the model to be inserted/updated.

Items 1 and 2 are working and I understand those concepts, but 3 is where I can't figure out how to do it properly. Here is a simplified version of my view:

Code:
<?php foreach($this->Admin_model->get_task_types() as $row): ?>
<?php echo form_open('tasktypes/insert'); ?>
<input type="hidden" name="ID" value="<?php echo $row->ID; ?>"/>
<input type="text" name="ttName" value="<?php echo $row->ttName; ?>"/>
<input type="submit" value="Update" />
</form>
<?php endforeach; ?>

Here is the relevant piece of my model, called Admin_model:
Code:
function save_task_types() {

        $db_array = array("ID" => $this->ID,
        "ttName" => $this->ttName);

        if ($this->ID) {
            $this->db->where("ID", $this->ID);
            $this->db->update("tasktypes", $db_array);
        }

        // If ID was not set in the controller, then we will insert a new record.
        else {
            $this->db->insert("tasktypes", $db_array);
        }
    }

Getting the data from my view to my controller is where I am lost. Based on examples I've seen in the forums, this is what I am trying in the controller:

Code:
function insert()
    {
    $this->load->model('Admin_model');

    $new_array = array(
        $ID => $this->$input->post('ID'),
        $ttName => $this->$input->post('ttName'));
            
        $this->Admin_model->save_task_types($new_array);
        
        redirect('tasktypes/display');
    }

Once I understand how this works I'll move on to learning how to validate, but I feel like I'm missing a core concept here. Any advice would be great.


Messages In This Thread
Problem sending post data to model for insert/update - by El Forum - 04-18-2009, 11:03 AM
Problem sending post data to model for insert/update - by El Forum - 04-18-2009, 07:58 PM
Problem sending post data to model for insert/update - by El Forum - 04-18-2009, 08:35 PM
Problem sending post data to model for insert/update - by El Forum - 04-18-2009, 09:25 PM
Problem sending post data to model for insert/update - by El Forum - 04-19-2009, 07:07 AM
Problem sending post data to model for insert/update - by El Forum - 04-20-2009, 08:29 AM
Problem sending post data to model for insert/update - by El Forum - 08-12-2009, 07:17 AM



Theme © iAndrew 2016 - Forum software by © MyBB