Archive for the ‘PHP’ Category

PHP Contact form

Monday, June 22nd, 2009

People, I know a PHP contact form is something that for newbie developers useally allways start trying to create as its pretty much essential on any website. I’ve written the code below to create one.

Here is a very easy way to create one in 3 simple steps:

HTML:

<form action=”mailer.php” method=”post”> <input name=”name” size=”19″ type=”text” />

<input name=”email” size=”19″ type=”text” />

<textarea cols=”30″ rows=”9″ name=”message”></textarea>

<input name=”submit” type=”submit” value=”Submit” />
</form>

PHP:

<?php
if(isset($_POST['submit'])) {

$to = “yourname@yoremailaddress.com”;
$subject = “New Email From Your Website’s name”;
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

$body = “From: $name_field\n E-Mail: $email_field\n Message:\n $message”;

echo “Data has been submitted to $to!”;
mail($to, $subject, $body);

} else {

echo “blarg!”;

}
?>

  1. Create 2 files in notepad/dreamweaver, 1 called form.php the other called mailer.php
  2. Change the to: and subject: in the mailer.php file to your requrements
  3. Upload it! Via FTP e.g CuteFTP, Filezilla, Cyberduck (Mac)

Its as easy as 1,2,3!