0

I want to create form in view file with array $data, like this:

Controller:

    $data = array(
    'name'        => 'username',
    'id'          => 'username',
    'class'   => 'form-control',
    'placeholder'        => 'username here',
);
$this->load->view('login_page', $data);

View:

<?php echo form_input($data); ?>

It doesn't work, I should use $name; $id; $class; $placeholder, but how to do it with form?

1
  • You passed data at view wrong way. use this way $this->load->view('login_page', array('data'=>$data)); Commented Jun 19, 2015 at 21:25

2 Answers 2

1

when you pass array to the view it turns array keys into variables. so for example in controller

$data['test'] = 'value';

if you pass $data to the view

$this->load->view('view_page', $data);

you wont be able to access $data array in the view, but rather - $test variable

so

echo $test;

would work fine

basically you need to insert your form data into another array like so

$data = array(
    'form_data' => array(
        'name'          => 'username',
        'id'            => 'username',
        'class'         => 'form-control',
        'placeholder'   => 'username here',
    )
);
Sign up to request clarification or add additional context in comments.

Comments

0

You just need to wrap the input config in an array so it doesn't get broken out into separate variables.

$data = array( 
        'formParams' => array(
              'name'        => 'username',
              'id'          => 'username',
              'class'       => 'form-control',
              'placeholder' => 'username here'
        )
        );

$this->load->view('login_page', $data);

View:

<?php echo form_input($formParams); ?>

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.