Codeigniter App Tutorial
Lección 4 de 10 · Codeigniter App Part 4 – Form Helper · TI y software
Codeigniter App Part 4 – Form Helper
all right so what I want to do now is create the login form in this box right here and we're going to do that by just creating a login view and we're going to
insert that login view into the main template in this area okay so we want to go to views and let's go ahead and create a new folder called users and our users views will go in here so let's
create a new file and save that as login dot PHP alright so in here we're going we're gonna use the form helper and like I said the documentation is great so if we say just see iPhone helper that
should give us the documentation page alright so in and I think the second video we added all our autoloaders and one of the helpers we auto loaded as the form so we don't have to include this if
you didn't ought alowed the form then you need to put this in somewhere in the class or somewhere in the view ok so that'll just load the form helper alright so what this does is gives us
pretty much a whole new syntax to create forms with and it's made to kind of simplify things and just encapsulate things so this here this echo form open this is equivalent to having a open form
tag alright so you can see right here this is the same as this alright what you want to put as a parameter is the page that you're sending to which is usually the controller and then a method
alright so this here would send to an email controller with a method of send alright and we can add attributes to our form if we want to add a class or an ID we can put those into a variable into an
array and then just pass that to the to the form open API all right let's see we have methods for adding hidden fields if your form is going to have I in a file upload button
then you need to use this instead of form open you want to use form open multi-part form input gives us input tags all right so we just echo form input and then add an array in there
with all the attributes so the name ID value all that stuff you can add in there all right so this here is equivalent to this form password text area upload dropdowns multi-select so
any form element you can think of is on this page and it will show you how to implement it all right so what we're going to do here is first create our attributes for our form and our
attributes is going to be equal to an array oh and by the way you don't have to use this form help but you could just as well put standard HTML form tags alright so the array let's give this
form an ID we'll give it an ID of login form and we're gonna give it a class we'll give it a class of form horizontal which is a bootstrap based class all right so
that's our attributes now we need to actually let's close that up let's echo the want to echo form open all right and in here we want to we want to we want to say where we want to send this and we're
gonna send this to a controller called user and a function called login or a method called login all right and then we also want to just pass in the attributes all right
all right so now what we want to do and let's close this form before we do anything else we're going to echo form clothes and that's equivalent to a closing form
tag so what we want to do here is add in our first input and we're going to use labels as well so I want to put this in a paragraph tag I want to say PHP echo form label alright so the form label is
going to say user name and now we want to create a an array of attributes for the input tag so PHP and we're going to create a variable called data equal to an array which will have our attributes
so the first thing will be the form name and the form I'm sorry not the form name the input name is going to be username let's see we want a placeholder placeholders just you know the text
that's there when you come when you enter the page so this would just say user username all right we want a style I'm just gonna add just an inline style just for the width so we're gonna say
width is gonna be 90% so basically 90% of the box that it's in and the last thing we need is a value now in code ignitor you can use this set value and what this does is it makes it so if you
submit the information and you have an error and you have to change something what this does is it sets the value that you already have in there so that you know you don't submit a form and have
everything go back to blank this will save your spot I'll save whatever you typed in username all right so that's that's really it for the name the username input it's just
make this look a little bit nicer all right so that looks a little nicer I am now what we need to do is a PHP echo form inputs and we just want to input that data array all right so the next
thing we need is a password so to make this go a little quicker I'm just going to copy this and I'll label the password the name is going to be password please holder
and then we'll set the value to password and this isn't going to be forum input it's gonna be forum password or else it would just be a plain text box okay we're just passing the data so that's it
for that now we just need our submit button all right so I'm gonna copy this and we're actually not going to have a label the name so the name will be submits let's see
it's not gonna have a placeholder I'm not gonna have style actually we're gonna have a class because we're using bootstrap I want the button to be formatted so we'll say class is going to
be equal to BTN BTM primary and for the value the value will just be login which is what the button will say and we change this to submit
all right so that should be our login form if we go and reload he don't you don't see it because we have an insert of the view yet so let's go to our layouts go to our main view and copy
this right here in sidebar content we want to say login users slash login wait a minute user slash login alright so hopefully that works yep so there's our login form I do want
to put a heading in there so let's go back to login and we'll put it in h3 all right so our login form is all set so I'm going to stop here and in the next video we'll actually create the
users controller and we'll have this form submit and we'll look at the form validation library as well
Ill show you how to use the form helper available with CodeIgniter. We will create a login form
The full source code is at – https://github.com/bradtraversy/mytodo_ci