|
Home » The tutorial » Contact Page Script
Contact Page ScriptBob's spent all this time on his site, but so far nobody knows how to contact him. For that we're going to add a simple contact form.
First, you'll need to make some changes to contact.php. Here's our new contact.php, but you can modify it however you want. Just keep the form elements with the same name.
<?php $pagetitle = 'Contact Bob Smith'; include('header.php'); ?> <h1>Contact Bob</h1> <form action="sendmail.php" method="post"> Name: <input type="text" name="name"><br> Email: <input type="text" name="email"><br> Message: <input type="text" name="message"><br> <input type="submit" value="Send"> </form> <?php include('footer.php'); ?> And we need to create a new file called sendmail.php.
<?php $sendto = 'you@yourdomain.com'; $subject = 'Web site contact form'; $from = $_POST['name'].' <'.$_POST['email'].'>'; $message = $_POST['message'];
$pagetitle = "Contact Bob Smith Photography";
include('header.php');
if (mail($sendto, $subject, $message, 'From: '.$from)) { ?> <h1>Thank you for your message.</h1> <p>Your message has been sent. Bob will be in touch soon.</p> <?php } else { ?> <h1>Problem sending message</h1> <p>Your message could not be sent. Please call Bob at 123-555-1212.</p> <?php }
include('footer.php'); ?> Notice that this file is actually pretty similar to the other pages we've built. A header and footer is included. The rest of the code deals with actually sending the message and displaying a message to the visitor that the email has either been sent or not. You probably should change that text to something more 'you'.
You will also need to change the $sendto variable to your email address and the $subject variable to whatever you want the email subject to be.
In action Well, sort of. This is the contact page on version 11 of Bob's site. Only catch is I've change it a little from what's here so that an email isn't actually sent. But you'll get the general idea if you want to play around.
|