Contact and Comment Forms in Movable Type
Jan 4th, 2006 by Karen
The last two days I spent most of my time developing a contact form in PHP for Movable Type. I didn’t have any trouble writing a plain old contact form. The problems all started when I moved it to Movable Type. The main issues is the fact that I can’t seem to get things to work by using dynamic templates. No matter what I do the form information doesn’t get properly past and thus the form is never executed. Since the page doesn’t change much this shouldn’t be an issue. Except the code for the side navigation uses PHP written for the rest of my pages which use dynamic templates and there is just enough difference between how it is written for dynamic templates and static templates that I can’t use the same code. One solution is to make a seperate module for the static version code and use that when necessary, another solution I worked on was writing PHP code to figure out if the page is static or dynamic and insert the proper code for that page. I got this working and in the process learned a bunch of nifty PHP code. My favorite little chunk of code was three lines that figure out what the current filename is.
<?
$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode(’/', $currentFile);
$currentFile = $parts[count($parts) - 1];
?>
Basically, I got the form to work by making it a statically created page and then adding code to insert the correct sidebar code. Since it took a considerable part of my day, I was happy to get it to work. The only problem was that I really wanted the page to be dynamic. So I took one last stab at fixing my dynamic code. First, I tested to see if I could pass a variable in the query string to a dynamic Movable Type page. That worked. Then I tested to see if I could pass a form variable from one dynamic Movable Type page to another dynamic page. This too worked which made me annoyed and jubilant all at the same. I mean, I tried this for hours yesterday with no luck. Finally, I changed my original code slightly so it grabbed the form data using $_REQUEST['fieldname']. Suddenly, everything worked as I had originally anticipated!
Part of my is not sure whether or not to be really mad or just thrilled that I got it working the way I wanted. Working with Movable Type and PHP has been a frustrating process for me. Mostly because I knew nothing about Movable Type before and a small amount of PHP. The more I work with it the more I learn though. Digging around the last two days has given me greater insights into how to solve a couple other issues I’m having with Movable Type as well as added to my knowledge of PHP. Perhaps tomorrow I’ll be able to fix the comment forms and search box so that they will behave as desired!
What would be really nice is if I could figure out some way to make this into a plugin so that people could just put in the proper settings and then insert a template tag where they want the form to appear. There is a plug-in to do this in Wordpress but alas nothing for Movable Type. When I get a few moments I may just have to write one. I took out the Libraries copy of Hacking Movable Type and it has a good chapter on writing plug-ins that may be able to help me out. At least that is my hope because I’d like for individual blogs to be able to set up their own mail server settings and each contact form to have its own To, From and Subject. Right now it feels like a pretty lofty goal considering how long it took me to get the plain old contact form to work in the first place! However, I won’t learn anything if I don’t at least give it a shot.
BTW, here is the final code for my contact form in case someone is interested in creating one for themselves. You need to have the PEAR Mail module and associated modules (Net_SMTP, Net_Socket) installed to make this work.
<?php
// The following parameters are pretty much all that you
// need to change except for the format of the email message
// below. Note that your mail server may require the mailTo
// address be in the host domain.
$mailTo = "emailaddresssendto@email.edu"; // The address that will receive form
submissions
$mailFrom = "emailaddresssentfrom@email.edu";
$mailSubject = "Web Form Response"; // Whatever you want
$mailHost = "Your_SMTP_Server"; // Usually looks like mail.yourhost.com
$mailPort = "25"; // Usually 25
// Get the form fields.
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];
$reqFields = $_REQUEST['required'];
$send = $_REQUEST['send'];
// I find including the time/date useful for record-keeping.
// Note that it is the web server’s time/date, not yours
// or the sender’s.
$date = date("l jS F Y, g:i A");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<$MTPublishCharset$>" />
<meta name="generator" content="Movable Type <$MTVersion$>" />
<link rel="stylesheet" href="<$MTBlogURL$>styles-site.css" type="text/css" />
<link rel="alternate" type="application/atom+xml" title="Atom" href="<$MTBlogURL$>atom.xml" />
<link rel="alternate" type="application/rss+xml" title="RSS
2.0" href="<$MTBlogURL$>index.xml" />
<title><$MTBlogName encode_html="1"$></title>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="<$MTBlogURL$>rsd.xml" />
<MTBlogIfCCLicense>
<$MTCCLicenseRDF$>
</MTBlogIfCCLicense>
</head>
<body class="layout-two-column-right">
<div id="container">
<div id="container-inner" class="pkg">
<$MTInclude module="banner module"$>
<div id="pagebody">
<div id="pagebody-inner" class="pkg">
<div id="alpha">
<div id="alpha-inner" class="pkg">
<?php
if (! is_null($send)) {
// A simple yet reasonably effective email address validator.
if ((!ereg(".+\@.+\..+", $email)) || (!ereg("^[a-zA-Z0-9_@.-]+$",
$email))) {
$errorMessages .= "<li>Invalid email address: $email</li>";
}
// Make sure required fields are filled in.
$checkFields = explode(",",$reqFields);
while(list($theField) = each($checkFields)) {
if(!$$checkFields[$theField]) {
$errorMessages .= "<li>Missing $checkFields[$theField]</li>";
}
}
// If there are errors, display them and a back button.
if($errorMessages) { ?>
<p>Errors were found on the form.</p>
<ul>
<?php echo $errorMessages; ?>
</ul>
<form name="contactform" action="contact_form.html" method="post">
<!– The following line specified which fields are required. List field names
EXACTLY as they are named in the form. –>
<input type="hidden" name="required" value="name,email,comments">
<p>Name: <input name="name" size="25" value="<?php
echo $name; ?>"> <span class="small">required</span></p>
<p>E-mail: <input name="email" size="25" value="<?php
echo $email; ?>"> <span class="small">required</span></p>
<p>Comments:</p>
<p><textarea name="comments" rows="5" cols="30"><?php
echo $comments; ?></textarea> <span class="small">required</span></p>
<input type="hidden" name="send" value="yes"/>
<p><input type="submit" value="Submit" name="submitform"><input
type="reset" value="Reset" name="reset"></p>
</form>
<?php
}
// No errors, send the message and print out success message.
else {
// Build the email.
$body = "Name: $name \n";
$body = $body . "Email: $email \n";
$body = $body . "—– Comments —–\n";
$body = $body . "$comments \n";
$body = $body . "——————–\n";
$body = $body . "Date: $date\n";
include("Mail.php");
$headers["From"] = $mailFrom;
$headers["To"] = $mailTo;
$headers["Subject"] = $mailSubject;
$params["host"] = $mailHost;
$params["port"] = $mailPort;
$mail_object =& Mail::factory("smtp", $params);
$mail_object->send($mailTo, $headers, $body);
?>
<h4>Thank you for submitting this form</h4>
<?php
}
}
else {
?>
<form name="contactform" action="contact_form.html" method="post">
<!– The following line specified which fields are required. List
field names EXACTLY as they are named in the form. –>
<input type="hidden" name="required" value="name,email,comments">
<p>Name: <input name="name" size="25"> <span
class="small">required</span></p>
<p>E-mail: <input name="email" size="25"> <span
class="small">required</span></p>
<p>Comments:</p>
<p><textarea name="comments" rows="5" cols="30"></textarea> <span
class="small">required</span></p>
<input type="hidden" name="send" value="yes"/>
<p><input type="submit" value="Submit" name="submitform"><input
type="reset" value="Reset" name="reset"></p>
</form>
<?php
}
?>
</div>
</div>
<$MTInclude module="sidebar module"$>
</div>
</div>
</div>
</div>
</body>
</html>

