Using PHP and forms in an iWeb web site

iWeb is a great tool for quickly laying out a website. However, it does not include support for using PHP in your iWeb site. Here I will show you how some tips and tricks for using PHP easily in an iWeb site, with an example also showing processing of HTML form data.

These tricks don't require any modifications to iWeb itself, but they do require access to configuration for your web server. I only provide examples for Apache here. The techniques shown here only require that Apache respects .htaccess files in your web directory, and that mod_rewrite is enabled.

Getting PHP to process iWeb's "HTML snippet" container.

iWeb includes the useful "HTML snippet" container in its GUI tools. This allows you to include arbitrary HTML on your iWeb page. Internally, iWeb puts the HTML content of your snippet into a separate file and includes this file as an inline frame on your page. If your page is at path /Home.html, then the content of the first HTML snippet on the page will be at /Home_files/widget1_markup.html.

In order to get the PHP engine to look for PHP code inside the widget1_markup.html file, I use Apache's very cool mod_rewrite. At the root level of my website, I create a file named .htaccess with this content:

RewriteEngine on
RewriteBase /
RewriteRule ^Home_files/widget1_markup\.html$ Home_files/widget1_markup.html [T=application/x-httpd-php,L]

If your server has a different handler configured for PHP, you'd need to replace the "application/x-httpd-php" in the above line with the type that applies on your server.

What this does is first tell Apache we want to use the rewrite engine on one or more URLs in this site. Then we tell the rewrite engine that files in the root of our site are at the URL "/". Then we use a regular expression to match the name of the file holding our PHP code, in this case "widget1_markup.html". (The ^ marks the start of the expression, the $ marks the end, and the \ in front of the dot makes it into a literal dot, rather than a wildcard.) We then rewrite the URL back to the *same URL*, BUT we tell Apache to treat the file as having a different MIME type. Use of this MIME type should cause the file to be processed by the PHP handler.

The simplest way to test the above is to put the following into your HTML snippet:

<?php phpinfo(); ?>

and then test it.

Processing forms

Using PHP to process forms inside an iWeb snippet is fairly easy, provide you stick to the design pattern of having the same PHP code output the unfilled form and process the filled-in form. The best way to do this is to have the PHP code look for one of the form variables, and if it set, then process the rest of the form. If it is NOT set, then output the HTML for the unfilled form. Here is an example:

$form_complete = 0;

if ($_REQUEST['submit'] == 'Submit') {
$form_complete = 1;

// Do what you want to with the form data here.


if ($form_complete == 0) {
<form name="myform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" target="_self">
<p>Please click this button to submit the form:
<input type="submit" name="submit" id="submit" value="Submit" />

There are a few important things to note about the above code.

  1. We use the POST method to submit the form, but GET could also be used.
  2. The form variables are accessed using the $_REQUEST array (which also includes cookies.)
  3. The action for the form is the same page. We generate this using $_SERVER['PHP_SELF'].
  4. The target for the form should be "_self". This loads the form results in the same inline frame as the original form. Without this, the results will open on a new, bare, page.

20 May 2008