Basic Forms with PHP

HTML forms are a great way to collect information directly from your users. There are plenty of input options available, even for the most basic, non-javascript enhanced forms. This post will go over the steps for creating, validating, and handling an elementary contact form with PHP and HTML.

We'll have to make a few basic assumptions before beginning. First, we'll assume that the page the form is displayed on can be executed with PHP. This could mean that either the page has a 'php' extension or the web server is configured to run this file as PHP, regardless of the file extension. Second, we'll assume that there is no conversion tracking (that is, analytics tracking the number of page visits vs the number of form submissions). This will make our submit logic a bit simpler. Finally, we will not be doing any javascript or ajax trickery. All the fields and submit request are handled as browser defaults.

Our initial step involves setting up a basic html form. We'll be submitting via the 'post' method, which passes the form parameters ...

read more »

Connecting to a MySQL Database with PHP

Working directly with the database is something I rarely do any more with the applications I work with. Instead of creating a raw connection and pulling data directly, I usually utilize some sort of customized data layer. (If you don't know what a data layer is, imagine creating a PHP application with no queries. You still have data being pulled, usually from MySQL table, but the data storage and structure is kept independent of your application. When done correctly, this can help immensely with code cleanliness and development.) However, setting up a connection and pulling information is still an important skill set, so here is a post about the basics. If you're looking for information that is more basic than a PHP-MySQL connection, you can read my series on the basics of the SQL language.

There are several ways to connect to a table, but I'd highly recommend using the MySQL Improved Extension, or mysqli, as a simple but powerful way to connect to your MySQL ...

read more »

How Many Tiers for a Clean View Layer?

An interesting possibility cropped up for my MVC a few weeks ago. I've been working with microformats on and off the last few months (you can read about one of my first adventures with them when I created a microformatted resume). I started to wonder where else my site could use this new feature. An easy place to add microformat tags are links - you can add a 'rel' attribute that describes the relationship of the link to your site. Also, if I added a layer of abstraction to my links site wide, then I could easy add behavior rules (target="_blank" and whatnot) that would affect an individual link across all of my pages and sites.

This idea would involve creating a table of all my links, internal and external, that could be mapped to meta information about the links. There would be a helper class on my site that would pull a link based on an alias, give it attributes based off of the meta data (including microformatting), and spit out a final link. No where on ...

read more »

Microformatting the Resume

When a computer script crawls across the web looking for information (like Google's search robot) they need to do a lot of thinking to figure out what information is located on a web page. After all, web pages are written by humans for humans, with the data and information contained in our complex, contextual language. Web developers can help a script understand their site with robots.txt, xml feeds, and designated syntax (like using h1 tags for headers), and more recently, microformats.

The best way a script can understand text is by tagging it. This is how some xml formats work - you wrap the title with a <title> tag, set relative priority with a <priority> tag, etc. However, xhtml and older html formats only let you wrap normal web pages with standard tags, like p (paragraph), h1 - h6 (headers), etc. Microformats uses attributes of tags (like class names) to help computers understand what the content of a page is instead of the actual tags.

I recently dove into microformats with my resume, located on my ...

read more »

Abstracting the Resume

Recoding all of my subdomains to follow a model-view-controller framework has been a really interesting process. I've had the opportunity to experiment with different design patterns and techniques, especially in the realm of data handling and content delivery. Abstracting out my resume is a great example of how I'm handling my data in a way that provides necessary functionality at the moment while staying flexible enough for future rewrites.

Until recently my resume was written inline with html tags, merely included into different web pages as needed. This was a bad solution - every time I wanted to change a piece of it I had to wade through html tags. I couldn't place any dynamic content in my resume or reuse it in non-html formats. A cleaner solution was needed.

My first step was to divide up my resume into pieces. I have five main categories: objective, job history, skills, achievements, and education. The objective is a single line of text while the other four categories are lists. Some of these lists are lines of text while ...

read more »

My Lifestream

As a web developer interested in creating a web presence I'm constantly looking for new projects to add to and inhance my websites. Some of these (like this blog) require a fair degree of committment to create new content, while others (like my about page) is a simple static page. I've always kept my eye on my social networks as a potential source of content for a new site, and the recent launch of my lifestream (lifestream site) fits my needs very nicely.

There's numerous examples of web developers using this technique on their sites. An obvious one is a display of a personal twitter feed on a page, usually on the home page or blog, as both a constant source of content and a way to collect possible followers. I've seen some more advanced uses too, including github activity.

I decided to do something a bit more ambitious with my lifestream. The concept of a 'lifestream' is a compilation of time-ordered online activity ( ...

read more »

Thoughts on Data Abstraction

Something that I've been working on a lot with both my work and personal web programming is data abstraction. The opportunity to work with several different handmade php frameworks has given me different ideas on what an effective data abstraction system should have. Without formal education, though, this post will be only based on my hands-on experience with the functionality I find myself using without true design patterns.

Data abstraction is a fancy word for treating the data as a separate entity from your web application logic. Web sites usually store their data in databases, using customized queries to pull information for their code to output and manipulate information. It doesn't matter what type of database or language you're working with; data abstraction can be employed whenever there is interaction with data (this post will be approaching this from a rough PHP - MySQL angle). Complex sites often have numerous queries scattered throughout the web site, and one needs to have intimate knowledge of the database structure during the ...

read more »

The Problem with Assuming

In the days of old (PHP 4) web programmers sometimes resorted to small shortcuts in their code. One of these was register_globals, which extracts the global arrays ($_REQUEST, $_POST, etc) into individual variables, allowing a programmer to write $name instead of $_POST['name']. This is an obvious security flaw - users can easily pass in custom parameters to overwrite variables in the code and cause unforeseen problems. In the below example, if register_globals was turned on in the PHP configuration, a tricky user can pass in a 'access' parameter and do some horrible things.

if($level == 'admin')
{
  $user = 'admin';
  $access = 'everything';
  $restrictions = 'none';
}

if($access == 'everything')
{
  // do some horrible things here
}

This is a good example of a bad programming practice - assuming that things will work the way you expect them to. That is, you assume that a level of 'admin' is the only way the 'access' variable could be set to 'everything'. The security-orientated approach would be to ...

read more »

Why (some) Client-Developer Relationships Suck

Some web development projects don't turn out. The blame falls on both the web developer's and client's shoulders, though most developers will pine about the 'Worst Client Experience Ever'. It doesn't matter if the project is related to a full-time position or a one-time contract... things don't always work out. A combination of misunderstanding and miscommunication can ruin the best of working relationships, pushing back deadlines and frustrating both parties. There are two main causes for why this happens.

Web Development is New

The entire field is brand new, and whether a developer has been working for the past few months or the last decade, it's important to treat it as such. While the majority of people understand how to use the internet, there's a great deal of mystery about the experience. This goes beyond users not understanding the difference between Flash and Javascript and straight to developers themselves. Payment, expectations, and techniques differ greatly between individuals and projects. Also, it's far too easy for a ...

read more »

Beginner Web Developer Mistakes

With my introduction into web development coming from a static html/css direction, I've had to overcome several bad coding habits over the years. Most of these started with poor assumptions about the way websites worked. It took a crash course in php programming and a few hard bumps before realizing how wrong my first websites really were. Here are a few of those assumptions and how I built on them to increase my programming skills.

Each URL relates to a unique file on a web server

If you're only used to working with static html files than this assumption is largely true. Every different URL that a user visits on a web page is in reference to a different file on the server. Without any type of scripting, the file is merely a chunck of html and can only be changed by a webmaster physically manipulating the document. My first dynamic website used php include commands to pull a fixed header and footer for all of my pages, allowing me to make global site changes from a single file. When I finally started to learn more about php variables ...

read more »