Welcome Guest, Not a member yet? Register   Sign In
Undefined variable / index fix
#1

Hello,

I am trying to program a form in Codeigniter 4 that I can use for both create and edit functionality.

For the edit function I use the variable $data to fill the fields, this variable is empty when you are on the create form.

This gives me an "Undefined variable: data" error. In Codeigniter 3 this was only an announcement but the website continued to work. With Codeigniter 4 I can't get the site to function with an empty variable.

Is there an easy way to avoid this error with an empty variable?

It is very annoying to make an extensive if isset function with every variable.
Reply
#2

Create a hidden field in the form that will hold the first variable of the array
use the value create or edit for that value now it should not give you an index
error.


PHP Code:
$data['mode'] = 'create';
// or
$data['mode'] = 'edit'

For the hidden field something like this:

PHP Code:
<input type="hidden" id="mode" name="mode" value="<?= $mode; ?>"
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB