2

I create MY_form_helper.php in application/helpers/, extending form_helper.php. The function is like this:

function alert($message,$type = 'success'){
    $data = array(
        'type' => $type,
        'text' => $message
    );
    return json_encode($data);
}

In my controller I call the function like this:

$this->load->helper(array('form', 'url'));
echo alert('Wrong email or password');

but it gives me an error

Message: Call to undefined function alert()

so what am I missing or doing wrong here?

Ying
  • 1,282
  • 4
  • 19
  • 34

2 Answers2

0

Place your MY_form_helper.php helper to application/helper

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

   function alert($message,$type = 'success'){
      $data = array(
        'type' => $type,
        'text' => $message
   );
   return json_encode($data);
}

and in your Controller place this code.

public function sample()
{
    $this->load->helper('MY_form_helper'); //load your helper

    echo alert('Wrong email or password'); //call your function
}

Let me know the result.

curiosity
  • 834
  • 8
  • 20
  • i did but still error. now it said Unable to load the requested file: helpers/my_form_helper.php – Ying Jul 17 '19 at 06:32
  • did you save your MY_form_helper.php helper to application/helper ? – curiosity Jul 17 '19 at 06:52
  • it suppose to be application/helpers right? yes, i put in that folder already. – Ying Jul 17 '19 at 06:57
  • that's right then did you load this? $this->load->helper('MY_form_helper'); to your controller? – curiosity Jul 17 '19 at 06:58
  • yes i try that already, but it give me others error said it's not found. since if i do that it will looking my file at system/helpers/ right? at least that what error said. – Ying Jul 17 '19 at 07:00
  • https://stackoverflow.com/questions/804399/codeigniter-create-new-helper click that link maybe it has a better explanation how to solve your problem. – curiosity Jul 17 '19 at 07:08
0

It should be working just fine as it is IF you stored it in the right directory which is application/helpers with the name MY_form_helper.php now you it should add the extension to the core helper:

$this->load->helper('form');
var_dump(alert('Wrong email or password'));

Tested

Sherif Salah
  • 2,085
  • 2
  • 9
  • 21