Welcome Guest, Not a member yet? Register   Sign In
Can't Pass Value from Form to Controller
#1

[eluser]Chris CMC[/eluser]
I'm a noobie and think I'm making another basic mistake. For some reason, all my fields are inputting as '0' when they enter the database.

I'm assuming that means the connection is working, and the model is actually inputting data. But somewhere in that process both the strings and integers are reduced to the integer 0.

Here's my view:
Code:
<?php
    echo form_open('welcome/input');
    echo form_input('title');
    echo form_textarea('body');
    echo form_input('user_id', '1');
    echo form_input('category_id', '1');
    echo form_submit('mysubmit', 'Submit Post!');
?>

Here's my controller:
Code:
function input() {

            $data = array(                
                'title' => $this->input->post('title'),
                'body' => $this->input->post('body'),
                'user_id' => $this->input->post('user_id'),
                'category_id' => $this->input->post('category_id'),
                );
            
            $this->load->model('Welcome_model');
            $this->Welcome_model->addOne($data);            
        }

Here's my model:
Code:
function addOne($data)
    {
        $this->db->set('title', $data['title']);
        $this->db->set('body', $data['body']);
        $this->db->set('user_id', $data['user_id']);
        $this->db->set('category_id', $data['category_id']);
        $this->db->insert('ci_entry');
        return;
    }

One last thing I should note is I have 10 fields in the ci_entry table, which is why I used 'set' for the four fields I'm actually using.

Any idea what's going on?

I've been banging my head the last two hours on this and can't figure it out.

(Again, I'm a noob ... my apologies for taking your time if this is obvious!)
#2

[eluser]bretticus[/eluser]
Looks fine to me at first glance (besides a missing closing form tag perhaps.)

Use the profiler class to get helpful information such as post data and database queries that were ran.
#3

[eluser]squarebones[/eluser]
Code:
function addOne($data)
    {
        $this->db->insert('ci_entry',$data);
        return $this->db->insert_id();
    }

Assigns the data to the correct rows without your intervention as long as the data values match the names of the rows in the table. Returns the id of the newly created row in case you need it for some reference (added bonus).
#4

[eluser]Chris CMC[/eluser]
Thanks Brett, the profiler is helpful. Here's the relevant part:

Quote:URI STRING
/welcome/input

POST DATA
No POST data exists

DATABASE: rightly QUERIES: 1
0.0579 INSERT INTO `ci_entry` (`title`, `body`, `user_id`, `category_id`) VALUES (0, 0, 0, 0)

The problem is that my form isn't passing the values to the controller. I just checked this again, by defining $data directly in the controller and passing it to the model. That works, so it's clearly a problem of getting the form to pass values to the controller.

With that I'm a bit stumped. I've checked the first parameter of form_open against the URI String above about ten times, but they're each what they should be.

Did I not configure something write in my install? Do I need to add a parameter to any of the form fields?

Any ideas?
#5

[eluser]bretticus[/eluser]
Check your html source on your form page.
#6

[eluser]Chris CMC[/eluser]
Here's the HTML:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
&lt;html&gt;
&lt;body&gt;
  &lt;form action="http://domain.com/CI/index.php/welcome/input" method="post"&gt;&lt;br/>
     &lt;input type="text" name="title" value=""  /&gt;&lt;br/>
     &lt;textarea name="body" cols="90" rows="12" &gt;&lt;/textarea><br/>
     &lt;input type="text" name="user_id" value="1"  /&gt;&lt;br/>
     &lt;input type="text" name="category_id" value="1"  /&gt;&lt;br/>
     &lt;input type="submit" name="mysubmit" value="Submit Post!"  /&gt;
&lt;/body&gt;
&lt;/html&gt;
I'm still pretty stumped on this.

Anything jump out at you?

Also: squarebones, thanks for the tip. Once I figure out how to get the data to the model, I'll clean it up accordingly. Thanks.
#7

[eluser]bretticus[/eluser]
Where's your closing form tag?
#8

[eluser]Chris CMC[/eluser]
Nice catch, I just added the close tag.

Unfortunately though it hasn't solved the main problem.

Still getting the same result from the profiler:

"POST DATA: no POST data exists"
#9

[eluser]ciGR[/eluser]
All looks fine.
But try to change your method name from input to another name.
Because when in your controller you call $this->input->post(); maybe try to call your method $this->input and not the CI class $this->input

I'm not sure that's the problem, but give it a try.
#10

[eluser]Dolrich[/eluser]
In your controller. The input function, there is an excess comma.
It shouldbe like this.

Code:
function input() {

            $data = array(                
                'title' => $this->input->post('title'),
                'body' => $this->input->post('body'),
                'user_id' => $this->input->post('user_id'),
                'category_id' => $this->input->post('category_id')
                );
            
            $this->load->model('Welcome_model');
            $this->Welcome_model->addOne($data);            
        }




Theme © iAndrew 2016 - Forum software by © MyBB