PHP DevCenter
oreilly.comSafari Books Online.Conferences.


PHP Forms

Using PHP to Interpret Forms


The Internet, as we know it today, is primarily the result of two seminal events:

  1. The February 1993 introduction of Mosaic, the first graphical Web browser (see references in the sidebar to the right), and,
  2. The 1994 addition of forms to the HTML specification.

Mosaic, with its multimedia capability, added entertainment value to what had previously been a bland character-based medium. Forms created the capability for dynamic Web sites tailored to user requests. Without either of these two developments, we would all probably be watching TV right now.

In view of the above, we'll start this column with a series on forms. Before getting down to the nitty-gritty, let's review our programming level assumptions and take a look at where we are going.

What I'm assuming

This column is not directed toward beginning programmers, although they are more than welcome to come along for the ride. I'm assuming our readers have a reasonable degree of programming experience. PHP experience is not assumed, but a background in a Unix-based language such as C or Perl would be very helpful. Whenever PHP deviates from typical programming conventions, I'll stop the show and take time to explain in detail. PHP arrays immediately come to mind.

Since embedded programming code segments in HTML documents is the main strength of PHP, it is also assumed you know HTML on a first name basis.

To get the most out of these sessions you will need access to PHP. Paraphrasing the old saying: "If your ISP doesn't offer PHP, move." If you need a low-cost PHP test bed, drop me a note at the address at the end of this article. PHP4 code is utilized in programming examples. I'll point out the differences between PHP3 and PHP4 when necessary. However, PHP4 has many advanced features and should be employed if at all possible. PHP is free, so there is little reason for your ISP not to upgrade.

Where we are going

The major goal of this series is to give you tools (program, function, or object) in each session that are immediately usable. When I do a series on a theme, as I'm about to do with forms, I'll build upon and expand on the material from previous sessions. My motto is "Start simply, and then dazzle them with my footwork!"

Add a little form to your life

When people start to get interested in Web-related programming, the first thing they typically want to do is a form. A form can be one simple input box on a search engine front end or a multi-page questionnaire.

The program form-one.php is the demonstration PHP program for our first exercise. Display the code here and cut and paste into your favorite editor, upload to your Web server, and run the script. If you have not uploaded PHP documents to your friendly ISP, check with your administrator to ascertain the extension required, typically php or php4.

Before going into code details, a few notes: PHP3 requires function declarations before they are called, so put the functions at the top of the script if you're trapped into PHP3. Also, the here document construct starting on line 44 is PHP4-specific so replace it with a multi-line echo statement.

PHP documentation calls the here document construct here print, but I prefer the Perl terminology since it's more descriptive. Notice I've used the here print construct to assign the big string to a variable, $HTML in this case. Some subsequent assignments are concatenated to $HTML and then printed. This minimizes I/O operations, which in turn increases program efficiency.

The here document construct is essentially a multiple line, double-quoted string. The new line character, \n, is automatically added to the end of every line. Perl programmers beware, there are three structural differences between the Perl and PHP versions of this construct.

The PHP version starts with three "<" characters, there is no semicolon termination after the starting label, and there is a semicolon after the terminating label. Anything that would normally appear between a double-quoted string may appear in this construct, including variables that will be expanded. The here document construct is a very convenient way of including a large group of HTML within a PHP block. Create a form with your favorite HTML editor and cut and paste it into a here document block as I've done in the example.

Pages: 1, 2

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: