Cannot include variables in a view that was defined in another view in codeigniter

I created a form_variables.php view that contains all the input form variables defined in a single file. Therefore, when I need to create an input field, I would just include the form_variables file and then use the form input variables defined in form_variables.php

Here it contains.

<?php 
$email = array(
    'name' => 'u_email',
    'type' => 'text',
    'maxlength' => '50',
    'class' => 'form-control',
    'value' => set_value('e_email'),
    'placeholder' => "Enter your Email Address"
    );

$pwd = array(
    'name' => 'u_pwd',
    'type' => 'password',
    'maxlength' => '50',
    'class' => 'form-control',
    'id' => 'pwd',
    'placeholder' => "Enter your Password"
    ); ?>

Now I have another view that contains the form.

<?php echo $this->load->view('includes/form_variables'); ?>
<div class="form-group">
    <?php echo form_input($email); ?>
</div>

He still says the $ email variable is undefined. Although it loads the form_variables.php file. Please, help.

+4
source share
2 answers

I have a better solution for this, this will solve your problem, and you can also create a dynamic field:

1- : common_helper.php /helpers. .

if (!function_exists('get_field')) {
function get_field($field, $data = array()) {
    switch ($field) {
        case "email":
            return array(
                'name' => 'u_email',
                'type' => 'text',
                'maxlength' => '50',
                'class' => 'form-control',
                'value' => set_value('e_email'),
                'placeholder' => "Enter your Email Address",
            );

            break;
        case "password":
            return array(
                'name' => 'u_pwd',
                'type' => 'password',
                'maxlength' => '50',
                'class' => 'form-control',
                'id' => 'pwd',
                'placeholder' => "Enter your Password",
            );
            break;
        case "custom":
            if (count($data)) {
                $placeholder = (isset($data['placeholder'])) ? $data['placeholder'] : 'Enter you text here';
                $length = (isset($data['length'])) ? $data['length'] : '50';
                $id = (isset($data['id'])) ? $data['id'] : '';
                return array(
                    'name' => $data['fieldName'],
                    'type' => 'text',
                    'maxlength' => $length,
                    'id' => $id,
                    'class' => 'form-control',
                    'placeholder' => $placeholder,
                );
            }
            break;
        default:
            return array(
                'name' => 'textfiled',
                'type' => 'text',
                'maxlength' => '50',
                'class' => 'form-control',
                'placeholder' => "Enter your text",
            );
    }
  }
}

: config/autoload.

, , .

get_field('password'),

<?php echo form_input(get_field('password')); ?>
, :

$fieldOpt=array(
  'fieldName' => 'username',
   //optional
  'placeholder' => "Enter your username here", 
  'id'=>'myidfield',
  'length'=>'60',
);    
<?php echo form_input(get_field('custom', $fieldOpt);?>

, .

+1

.

Class form_variables extends CI_Controller
{ 
    function get_email_field()
    {         
        return array(
                   'name' => 'u_email',
                      'type' => 'text',
                     'maxlength' => '50',
                      'class' => 'form-control',
                     'value' => set_value('e_email'),
                    'placeholder' => "Enter your Email Address"
                  );
    }

    function get_password_field()
    {         
         return array(
                 'name' => 'u_pwd',
                 'type' => 'password',
                  'maxlength' => '50',
                  'class' => 'form-control',
                  'id' => 'pwd',
                  'placeholder' => "Enter your Password"
              );
    }
}

       $this->load->library('../controllers/form_variables');
       // use your function 
      $email_field =  $this->form_variables->get_email_field();
      $pass_field =   $this->form_variables->get_password_field();

, .

+3

All Articles