John Harper
John Harper

Reputation: 319

"Notice (8): Undefined index: " cakephp

I am building a pdf conversion utility for my user. I am working in CakePhp and my controller is receiving Ajax call. Why i am getting Notice:8 error

Controller:

public function convertToPdf() {
            $this->autoRender = false;
            $pdf = new WkHtmlToPdf;
            //$this->RequestHandler->respondAs('json');
            // echo $convertData = json_encode($inputVal);

            if ($this->RequestHandler->isAjax()) {
                $pdfName = uniqid();
                if ($_FILES['conversionSourceFile']) {
                    echo "File";
                    $pdf->addPage($_FILES['conversionSourceFile']['tmp_name']);
                } elseif ($_POST['conversionSourceUrl']) {
                    echo "Url";
                    $pdf->addPage($_POST['conversionSourceUrl']);
                } elseif ($_POST['conversionSourceHtml']) {
                    echo "Html";
                    $pdf->addPage('<html>' . $_POST['conversionSourceHtml'] . '</html>');
                }

                $saveToPath = 'upload/' . $pdfName . '.pdf';
                if ($pdf->saveAs($saveToPath)) {
                    echo 'upload/' . $pdfName . '.pdf';
                }
            }
        }

Error: Notice (8): Undefined index: conversionSourceFile [APP/Controller/PdfsController.php, line 42] Code Context

if ($this->RequestHandler->isAjax()) {
            $pdfName = uniqid();
            if ($_FILES['conversionSourceFile']) {

PdfsController::convertToPdf() - APP/Controller/PdfsController.php, line 42
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 486
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 187
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 162
[main] - APP/webroot/index.php, line 109

Upvotes: 1

Views: 14437

Answers (4)

user4005304
user4005304

Reputation: 11

I solved the error "Notice (8): Undefined index" changing the order var $ uses.

before:

class GastosController extends AppController {  
    var $uses = array('Comprobante','Gasto','TipoGasto');

... ... ..

After:

class GastosController extends AppController {  
    var $uses = array('Gasto','Comprobante','TipoGasto');

.. .. ..

Put the name that corresponds to the class (Gasto) first.

Upvotes: 1

Manish Chauhan
Manish Chauhan

Reputation: 570

To avoid notice in your code you have to use isset() OR !empty().

Using isset() and !empty() you can check whether variable is set and does not have an empty value.

e.g,

if (isset($_FILES['conversionSourceFile'])) {
    // your code
}

OR

if (!empty($_FILES['conversionSourceFile'])) {
    // your code
}

Upvotes: 4

adriaroca
adriaroca

Reputation: 172

You need to check the first condition, with an "isset" or "!empty ()", like:

if(isset($_FILES['conversionSourceFile'])){...}

Upvotes: 2

Vahid Hallaji
Vahid Hallaji

Reputation: 7447

Simply check to isset $_FILES superglobal variable to prevent notice when not set.

if (isset($_FILES['conversionSourceFile'])) {
    // Do more stuff
}

Upvotes: 3

Related Questions