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:
If your server has a different handler configured for PHP, you'd need to replace the "
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:
and then test it.
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:
There are a few important things to note about the above code.
20 May 2008