PHP Classes Ex. #3: Modify Class and Validate Data

If you completed PHP Classes Ex. #1 and #2, you have now written a working user registration form. Time to tweak it and make it better.

First of all, it would be preferable to have the message --Select one-- at the top of each select field. Add a line to the makeSelect() method to accomplish this. The value should be No response. You won't need the "None" value at the top of the $browsers array, so delete that. With this change to class Select, you can see how using a class can simplify your work. One line of code, and all the select fields update.

Your user responses won't be very useful without some basic information, so the next task is to make three of the fields required. Above the form, add * Indicates required field. Then add an asterisk to the Name, Username, and Email fields.

Next, add code to validate the data in those three fields. This code will appear in the second half of the script, after you have retrieved data from the $_POST[] variable. The function empty() will let you know if there is data in the field. To help the user supply missing information, include a back button with the error message. (If you completed Forms Ex. #4, you have already written one of those.)

The email field is a special case. Not only can you check for the presence of data, you can check for an @(at symbol), which would be included in any valid email address. So here the data must satify two conditions to be acceptable. You can use the strpos() function to confirm the presence of the @ character.



Thank you very much for making this wonderful website. I really enjoyed all of the exercises especially the Arrays and Classes (I'm having problems dealing with them at first), now I have an understanding of how they work and how to manipulate them. I'm pretty much a hands on learner so its a big help for me and also this site gave me more confidence in PHP. Now I think I'm ready for more, will find something interesting and practice a lot.

I hope you add more exercises, more complex ones will be great. will check this site from time to time and again Thank you very much!

Congratulations on finishing the exercises!

It was really helpfull ! Thanks for initiate me in PHP is amazing what you can do

Kind Regards!

Thanks for this site and exercises !!

Please inform us via email if you get new ones