Fork me on GitHub

Nibble Development

Nibble Development

PHP classes and libraries for developers

Adding data to the form

Some times it can be necessary to add data into a form for the user. An example of this is in a blog, if the user wants to edit a blog post, the edit form must have the contents of the current blog post added into it for the user to edit. Nibble forms has a simple PHP method for adding data as an array. Each item in the array needs a key and value. The key must be the name of the field that the data is to be added to, the value must be the data to be added. Thus if a form has fields “username” and “telephone”, and the data “Luke” and “0000800863″ need to be added respectively, the PHP method call will look like this:

Data can also be added for checkbox and multi-item select fields, however the array must be slightly different. As checkboxes and multi selects can have more than one selected/checked item, the array must contain an array of values for the key to either of these fields. So if a form checkbox field “vehicles” has the values “car,bike,lorry,van” and “car,van” need to be added as checked the PHP method call will look like this:

The setup of this method means that if a database has columns in a table named the same as the form fields, the queried data can be added straight into the form without manually writing the array. For example, a form has the fields “username”, “telephone”, “email” and “password” and a database table has the same columns, named the same, in the table “user”. To load the user into the table, a simple code snippet might look like:

Luke Rotherfield

Freelance PHP Developer
is a PHP developer dedicated to writing the most awesome liraries for his fellow devs :) enjoy

Latest posts by Luke Rotherfield (see all)

Leave a Comment