Pages

Saturday, April 24, 2010

HTML Comes Before PHP

Building a web site that uses a form almost guarantees that there will be a use of PHP. Depending on the scope of the form, switching in between PHP and HTML can be confusing, and messy. In order to avoid this, it's best to build the HTML side before coding any PHP.

It doesn't matter how long a web programmer has been at it. They will always occasionally forget to close a tag, or maybe add one too many tags in. Plus, with no PHP to get in the way, testing the form's appearance is a simple task. If it doesn't look right, just throw in some CSS to give it a good design. It is highly recommended that the form is not echoed out using PHP. The only thing seen is a small PHP icon, instead of the actual form, making editing the form difficult.

Once the form is completed, this is where the PHP comes in. Before any HTML code is where the first PHP code should be. This is the place to get all variables that were just posted and check to make sure that the form has been completed and any fields that are required have been filled in. This way, the form can then display any errors or missing fields using CSS.

If the form didn't validate, it would be a pain for the user to re-enter all that information again. Since the web page already has that information, it's easy to tell the form how to display it. In each input item, create a value of =$some_var_name?>. This way looks a lot neater than echoing out the variable. When testing from a hard drive instead of a server, the browser may not show the variable value, but it will show the variable that will display there.

What if certain users can see different fields? This can sometimes be the case for web sites that have a log in system. Again, in a case like this, code the HTML part first and make sure it is working and looks good. Then, open and close a PHP section before the field you wish to restrict, doing the same after the field as well. In the field above the field, type the code that will determine which users can access this field. This will usually end with an if statement. Every time a squiggly bracket is opened, in the PHP code below the field, close it right away. This will help prevent breaking of code.

After the page is completed and the PHP is working perfectly, what happens if another field is required? Because of the clean layout that has already been coded, it is easy to insert the HTML input field, and throw in a bit of PHP to make it work with what has already been made.

ShareThis

No comments:

Post a Comment