A couple of days ago I created a Forms class that I can re-use in most of my projects. I stated that I would show you an example of how I generate the form fields when they're needed. So here we go.
To reference the Form Class:
http://dinocajic.blogspot.com/2015/06/php-forms-class.html
In the following example I'm creating a form and utilizing the forms class to help me separate most of the HTML from PHP.
This is what the form will look like once the code is executed.
To help guide you through the code:
Later, I'll generate a class that ties all of it together.
To reference the Form Class:
http://dinocajic.blogspot.com/2015/06/php-forms-class.html
In the following example I'm creating a form and utilizing the forms class to help me separate most of the HTML from PHP.
This is what the form will look like once the code is executed.
To help guide you through the code:
- $_form is instantiated prior to the method call.
- A random token is generated and set to $_SESSION['token'] to prevent Cross Site Request Forgery
- Also as an additional measure of security, we're only going to allow for the form to be valid for 5 minutes. So, we'll set $_SESSION['token_time'] = current time()
- The hidden field is generated with the random token as the value
- Input type of text is called to generate po_number..
- Months are generated (i.e. 1 => January, 2 => February, etc).
- Select tag is generated and the months are passed into it as the options
- Days are simply generated as 1 to 31 and are passed to a new select tag
- Two years are added to the current year and are passed to another select tag.
- Locations are passed through to a select tag. This ideally should be stored in the Database but for the sake of the example, we added the locations manually to the array before passing it to the select tag
- Status array is generated (another good example of items that should have been stored in the database) and passed to another select tag.
- Submit button is generated
- Form is ended.
- Also please note that the form() method is part of a class. I just created a snippet so that you can focus on the method() itself
Later, I'll generate a class that ties all of it together.
Comments
Post a Comment