• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input->post not working

#1
[eluser]zenonn[/eluser]
hi,
I am new to CI, and seem to having a strange issue. I can use the $_POST var to get values from forms. As soon as I try to use input->post('var'), I get nothing to returned to the controller.
Here is the code

Controller:
Code:
function customer_insert()
    {
        
        
        $first_name = $this->input->post('first_name');
        $last_name = $this->input->post('last_name');
        $email = $this->input->post('email');
        $phone = $this->input->post('phone');
        $address = $this->input->post('address');
        $city = $this->input->post('city');
        $state = $this->input->post('state');
        $zip = $this->input->post('zip');
        $account = $this->input->post('account');
      
        
        $data = array(
        
        'first_name' => '$first_name',
        'last_name'  => '$last_name',
        'email' => '$email',
        'phone' => '$phone',
        'address' => '$address',
        'city' => '$city',
        'state' => '$state',
        'zip' => '$zip',
        'account' => '$account',    
        
        
        
        );
        
    
        $this->account_model->insert_customer($data);
        
        
        redirect('customers/accounts');
    }
Model:
Code:
<?php
class Account_model extends Model
{
     function __construct()
    {
    parent::Model();
    }
    
    function get_all_accounts($word)
    {
    echo "Database". ' ' .$word;    
    $myData = $this->db->get($word);
    return $myData->result();
    }
    
    function insert_customer($data)
    {
        
        
        $this->db->insert('customers',$data);
    }
}

?>
form:
Code:
<div id="form">
            &lt;?=form_fieldset();?&gt;
            <legend>Customer Account Details</legend>
            &lt;?= form_open('customers/customer_insert');?&gt;
            <p>x
            &lt;?=form_label("First Name:(Required)","first_name");?&gt;
            &lt;?=form_input('first_name','');?&gt;
            </p>
            <p>
            &lt;?=form_label('Last Name:(Required)','Last_name');?&gt;
            &lt;?=form_input('last_name','');?&gt;
            </p>
            <p>
            &lt;?=form_label('Email:(Required)','email');?&gt;
            &lt;?=form_input('email','');?&gt;
            </p>
            &lt;?=form_fieldset_close();?&gt;
            &lt;?=form_fieldset();?&gt;
            <legend>Customer Address Details</legend>
            <p>
            &lt;?=form_label('Phone:(Required)','phone');?&gt;
            &lt;?=form_input('phone','');?&gt;
            </p>
            <p>
            &lt;?=form_label('Address:(Required)','address');?&gt;
            &lt;?=form_input('address','');?&gt;
            </p>
            <p>
            &lt;?=form_label('City:(Required)','city');?&gt;
            &lt;?=form_input('city','');?&gt;
            </p>
            <p>
            &lt;?=form_label('State:(Required)','state');?&gt;
            &lt;?= form_input('state','');?&gt;
            </p>
            &lt;?=form_label('Zip Code:(Required)','zip');?&gt;
            &lt;?=form_input('zip','');?&gt;
            &lt;input type="hidden" name="account" value="&lt;?= rand(1000,50000); ?&gt;"&gt;
            <p>
            &lt;?=form_submit('','Submit Data');?&gt;
            </p>
            &lt;?=form_fieldset_close();?&gt;
            &lt;?=form_close();?&gt;

#2
[eluser]TheFuzzy0ne[/eluser]
I can't see where you've opened your form... Am I missing something?

EDIT: I take that back. I saw the fieldset first and assumed you didn't open the form, as fieldsets should be inside the form.

EDIT2: OK, I think this has something to do with your nesting. You open a fieldset, then the form, but then you close the fieldset before the form. Switching the form_open, and form_fieldset should fix it?

#3
[eluser]zenonn[/eluser]
Hello,
Thanks for reply. I fixed the nesting problem and the issue still only sends $first_name,$last_name etc. I tried a form with no nesting and still getting just empty vars like this $email.
Thanks

#4
[eluser]TheFuzzy0ne[/eluser]
My suggestion would be to validate your page. Once you are sure it's valid HTML, it's a lot easier to troubleshoot. Smile

#5
[eluser]zenonn[/eluser]
I fixed the problem. I was putting '' quotes around the my post vars which turned them into strings. I feel real dumb. lol
Thanks for the help
Chris

#6
[eluser]TheFuzzy0ne[/eluser]
Oh I see. I can't believe I didn't spot that. Well done for solving it. I'll bet you won't do that again in a hurry.

#7
[eluser]zenonn[/eluser]
lol, that was the problem I was in hurry trying to do a simple thing.
wasted 4 hours yesterday ouch
. :blank:

#8
[eluser]meigwilym[/eluser]
Just a suggestion for tidier code, put the post vars straight in your $data array.

Code:
$data = array();

$data['first_name'] = $this->input->post('first_name');
$data['last_name'] = $this->input->post('last_name');
$data['email'] = $this->input->post('email');
$data['phone'] = $this->input->post('phone');
$data['address'] = $this->input->post('address');
$data['city'] = $this->input->post('city');
$data['state'] = $this->input->post('state');
$data['zip'] = $this->input->post('zip');
$data['account'] = $this->input->post('account');

Mei

#9
[eluser]zenonn[/eluser]
Yes good point
thanks Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.