1
votes

i cannot get the post value from the form it inserting empty values in database

user controller

public function edit($id = NULL) {
    if ($id) {
        $this->data ['user'] = $this->user_login_model->get($id);
        count ( $this->data ['user'] ) or $this->data ['errors'] [] = "user could not be found";
    } else {
        $this->data ['user'] = $this->user_login_model->new_user ();
    }

    $rules = $this->user_login_model->rules_admin;
    $id || $rules ['password'] .= '|required';
    $this->form_validation->set_rules ( $rules );
    if ($this->form_validation->run () == TRUE) {

          $data=$this->user_login_model->array_from_post(array('name','email','password'));
        $this->data['password']=$this->user_login_model->$data['password'];
        $this->user_login_model->save($data,$id);
        redirect('admin/users');

        // do stuff

    }

if it's a new user then it will post new data

public function new_user() {
    $user=new stdClass();
    $user->name ='';
    $user->email ='';
    $user->password ='';
    return $user;

    var_dump($user);

model method which will loop through the array and return it

public function array_from_post($fields) {
    $data = array ();
    foreach ( $fields as $field ) {
        $data['feild']=$this->input->post($field);
        return $data;
    echo var_dump($data);
    }
}

this is the var_dump

object(stdClass)[22]
public 'id' => string '18' (length=2)
public 'email' => string '' (length=0)
public 'password' => string '' (length=0)
public 'name' => string '12' (length=2)

this is empty , can you tell me what is the problem ?

2
I guess your this line is wrong $data['feild']=$this->input->post($field); This should be $data[$feild]=$this->input->post($field);Shaiful Islam
i tried both not workingCyed Safir
did you tried with $data[$field]=$this->input->post($field); besides of $data[$feild]=$this->input->post($field);?user1823693
When you say the var_dump is empty, which one do you mean? The one is new_user() or the one is array_from_post()Craig

2 Answers

0
votes

Try this
Your controller edit function

  public function edit($id = NULL) {
    if ($id)
    {
        $this->data ['user'] = $this->user_login_model->get($id);
        count ( $this->data ['user'] ) or $this->data ['errors'] [] = "user could not be found";
    }
    else
    {
        $this->data ['user'] = $this->user_login_model->new_user ();
    }

    $rules = $this->user_login_model->rules_admin;
    $id || $rules ['password'] .= '|required';
    $this->form_validation->set_rules ( $rules );
    if ($this->form_validation->run () == TRUE)
    {

        $this->data=$this->user_login_model->array_from_post(array('name','email','password'));
        $this->data=$this->array_from_post(array('name','email','password'));

        // I removed this line not sure what you did here
        $this->user_login_model->save($this->data,$id);
        // do stuff
        redirect('admin/users');

    }
}

Your model array_from_post function

public function array_from_post($fields)
{
    $data = array ();
    foreach ( $fields as $field )
    {
        $data[$field]=$this->input->post($field);

    }
    return $data;

}

Your new user function

 public function new_user() {
    $user=new stdClass();
    $user->name ='';
    $user->email ='';
    $user->password ='';
    return $user;
}

Hope it will help you

0
votes

Your array_from_post function is basically useless. You have a return INSIDE the loop, meaning you abort the loop/return from the function the FIRST time you try to iterate the loop. So you'll only ever return ONE of your argument values.

e.g.

array_from_post(array('foo', 'bar'));

foreach(array('foo', 'bar') as $field) {
   return $field;
}

You start the loop, foo gets assigned to $field, and then you return. The loop will NEVER reach bar.

You probably want

public function array_from_post($fields) {
    $data = array ();
    foreach ( $fields as $field ) {
        $data[$field]=$this->input->post($field);
    }
    return $data;
}

Note $data[$field]. Using $data['feild'] will simply give you an array with ONE value in it, the last one which got processed by the loop. And note how the return call is OUTSIDE of the loop.