Welcome Guest, Not a member yet? Register   Sign In
Pass form fields from application to view?
#1

Hi,
my question: is it possible to pass a hole form field from application to a view?
Example: 
In the view I want to print out a drop down field,
PHP Code:
form_dropdown('project_type_id'$dropNULL
but I want the entire string to be created in the controller so in the view I can print something like
PHP Code:
echo $form1

Why? I have a lot of form fields to display. These are dynamically created from a database and the view file does not know what type of field it is. I can of course send information about that but it will be simple just to echo one variable than som switch statement and all form field types output.

/Pelle
Reply
#2

Yes, you can save the output of form_dropdown to a variable:
PHP Code:
$html_form form_dropdown(...); 

And you can create the whole form:

PHP Code:
$html_form form_open(...) . form_dropdown(...) . form_submit(...) . form_close(); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#3

Thank you!
The code in view will be much much better.

Working simple example:
In controller:

PHP Code:
$array_with_options = array('1' => 'First option''2' => 'Second option');
$data['html_form'] = form_dropdown('some_name_on_filed'$array_with_options['data'], 'selected_line'); 

In view:
PHP Code:
echo $html_form
Reply
#4

basically you can just write everything in controller and just echo the html.
pro novice
Reply
#5

(03-09-2020, 07:27 AM)tweenietomatoes Wrote: basically you can just write everything in controller and just echo the html.

Yes but it’s not best practice to echo directly in controller and it doesn’t follow the MVC pattern. For a quick test or for debugging it’s ok we all do it. But for the rest, it’s always better to create a view.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB