Arquivo Eletrônico


2010, May-03

Use a PHP Form in iWeb

There is a new post explaining how to password protect an iWeb site. You can find it here

I recently needed to include a form to a customer’s web site and I wanted it to have some kind of input validation and security against being used for spam purposes. What I really needed was to include some javascript and PHP code in the form, but how to do it in iWeb?

Here I will explain how I was able to do it and may be you could adapt what is shown here for your own needs. This instructions have been tested to work with Dreamhost, your mileage may vary.

HOW IT’S DONE

iWeb includes the useful “HTML snippet” container in its GUI tools. This allows you to include arbitrary HTML on your iWeb page, including javascript and PHP. Internally, iWeb puts the HTML content of your snippet into a separate file and includes this file as an inline frame on your page.

Let’s say your page is at path /Home.html, then the content of the first HTML snippet for this 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, we will use Apache’s directive AddHandler. Create a file named .htaccess and place it in the folder where the widget1_markup.html file resides, in our example, /Home_files. The content of the .htaccess file will be:

AddHandler php5-script .html

That is it. That is the only line that we need to add to the .htaccess file. What this does is to instruct Apache to process all the .html files in the directory by the PHP parser as if the file had the .php extension. We could have placed the .htaccess file at the root level of the website, but that would mean that every single .html file in our site would be processed as .php and that may break some things. By placing the .htaccess in the folder where the widget1_markup.html file resides will guarantee that only that file will be processed as .php.

THE FORM

Using PHP to process forms inside an iWeb snippet is fairly easy, provided that 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 the form variable $_POST[‘submit’], and if it set, then process the rest of the form. If it is NOT set, then output the HTML for the unfilled form. In the example below we are using javascript and PHP to validate the form data and to avoid having the form hijacked by spammers.

<!--
// CSS goes here
-->

<script type="text/javascript"><!--mce:0--></script>
<table border="0" cellspacing="1" cellpadding="0" width="400" align="center">
<tbody>
<tr>
<td>
$error';
if($success) echo '<span style="color: green;">We have received your contact information</span>';
if($success) echo '<script type="text/javascript"><!--mce:1--></script>';
?></td>
</tr>
<tr>
<td>
<form method="post">
<table border="0" cellspacing="1" cellpadding="3" width="100%">
<tbody>
<tr>
<td width="16%">Full Name</td>
<td width="2%">:</td>
<td width="82%">
<input id="customer_name_id" name="customer_name" size="50" type="text" value="<?php echo $form_name ?>" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td>
<input id="customer_mail_id" name="customer_mail" size="50" type="text" value="<?php echo $form_mail ?>" /></td>
</tr>
<tr>
<td>Telephone</td>
<td>:</td>
<td>
<input id="customer_phone_id" name="customer_phone" size="15" type="text" value="<?php echo $form_phone ?>" /></td>
</tr>
<tr>
<td>Message</td>
<td>:</td>
<td><textarea id="detail_id" cols="50" rows="10" name="detail"><?php echo $form_msg ?></textarea></td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<input id="send" onclick="return checkForm();" name="submit" type="submit" value="Enviar" /></td>
</tr>
</tbody>
</table>
</form></td>
</tr>
</tbody>
</table>

You can download the required files at the end of the example.

php_form_iweb.zip




Attachments:

Php form iweb


13/14