0

This has made me confused. I have an input element in form. Then, get the value for adding data into database

public function actionCreate()
{
    $model = new Departemen;
    echo $_POST['nama_departemen'];


        $model->nama_departemen =  $_POST['nama_departemen'];
        $model->keterangan = $_POST['keterangan'];

        if($model->save())
        {
            $this->redirect('departemen/actionIndex');
        }

}

But an error occur in the browser

Error 500 Undefined index: nama_departemen

I am very confused because it is successfully inserted into database. How I fix it?

4
  • Maybe there's no input named "nama_departemen". What does "echo $_POST['nama_departemen']; " this prints? Commented Dec 15, 2015 at 10:49
  • comment echo $_POST['nama_departemen']; And I suspect your action is index and it should be $this->redirect(array('departemen/index')); Commented Dec 15, 2015 at 10:53
  • Actually $_POST['nama_departemen'] already exist. And its value can inserted into database. Commented Dec 15, 2015 at 10:58
  • 1
    why you are not using model ? echo $_POST['Departemen']['nama_departemen']; Commented Dec 15, 2015 at 11:04

2 Answers 2

1

try this:

public function actionCreate()
{
    $model = new Departemen;

    if(isset($_POST['nama_departemen'])){

        echo $_POST['nama_departemen'];


        $model->nama_departemen =  $_POST['nama_departemen'];
        $model->keterangan = $_POST['keterangan'];

        if($model->save())
        {
            $this->redirect('departemen/actionIndex');
        }
    }

}

and to make sure that the index is posted, try to var_dump($_POST) and share us the result;

Sign up to request clarification or add additional context in comments.

Comments

0

First you should verify the value exists before proceeding to assign it to model value as pointed out by Mohammad. If it saves on the database then your undefined index is on actionIndex where its redirected to after saving.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.