Search form

Follow:

PHP Classes Ex. #1: Select Field Class and Object

In this PHP exercise, you will build the beginnings of a user registration form. To do this, you will create a class for making the select field, then use an object derived from the class in the form. You can begin with the script you wrote for Functions Ex. #6, since you will be revising the two functions you created there as methods of your class.

Then begin to write the class Select. You will need two properties, $name for the name of the select field, and $value, an array to provide the option values. You will also need four methods in addition to the two methods you will adapt from Functions Ex. #6: setName, getName, setValue, getValue. Checking to be sure the value is an array belongs in the setValue method, so write that here, and delete it from from makeSelect if you are using your script from Functions #6.

Now we come to the two functions you wrote to generate the select field for Functions Ex. #6. Change the makeOptions function to iterate over the array argument's values rather than keys. This will be your fifth method. Then revise the makeSelect function to be the sixth method in your class.

Next comes the HTML. Write a user registration form asking for name, username, email, browser. Use text fields to collect the user data for the first three, then instantiate an object based on your class for the select field. When the user clicks the submit button, return the data as confirmation.

If you were creating a registration form to use on the Web, you would want to collect the data in a database. However, using PHP with mySQL or other databases is beyond the scope of this website.

Readers: In case you're not familiar with the syntax highlighter's functions, just click 'expand source' in pogimesabiniate's post to see his solution.

I haven't had time to check whether your script works, but I can see that you did not follow the instructions in the exercise. You might be able to get this script to give the same output with just those two methods, but this script serves as the basis for the next two exercises, which need a more developed class. Also, your class does not include validation.

The four methods you left out are standard to many classes, so it will better prepare you to write classes in the future if you rewrite your script to include them.