logo-dw

by Ramandeep

Raman runs a Design Agency in Birmingham, UK. His company provides quality web services to clients all over the world.

Using Dreamweaver and PHP to send form results in an email

The form is a primary way to get web interactivity and structured communication. Dreamweaver has a robust and superior implementation of HTML forms. Forms enable user not only to read information passively from the screen, but also to send information back.

One of the more common questions is how to submit a form to send information in an e-mail. Using PHP, it's really very easy. Here we'll learn about processing forms to send e-mail using PHP.

Getting started with forms in Dreamweaver

I am using Dreamweaver MX 2004 but the options are almost same in the old and latest releases of Dreamweaver. We will be making a simple contact form and will deliver the results in an email using a PHP script.

Step 1: Create a form

We have created a simple form with four form fields as:

  • Name (text field)
  • Last name (text field)
  • Email address (text field)
  • Comments (textarea)

contactform (9K)

Step 2: PHP Script for processing the form results

We will use the PHP to send our form results through an email.

Copy and save this code in a file sendresults.php and set the following parameters the according to your requirements.

  • $subject (Subject of email sent to you)
  • $emailadd (Your email address. This is where the form information will be sent)
  • $url (Where to redirect after form is processed. )
  • $req (Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.)

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'Results from Contact form';

// Your email address. This is where the form information will be sent.
$emailadd = 'mail@rdsnetworks.net';

// Where to redirect after form is processed.
$url = 'http://www.rdsnetworks.net/confirmation.html';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

Step 3: Pointing your form to the PHP script to process it

Select the form and in form properties you will see the following attributes.

form-properties (8K)

The Name attribute is very clear gives your form a name. We have given our form name as Contact form.

The Action attribute determines what should be done with the form content. Most commonly, action is set to a URL for running a specific web application or for sending email.

Here you will give the URL where you saved your sendresults.php file Example www.yourdomain.com/sendresults.php

The Method attribute tells the browser or the web server how to present the form contents to the application that will process the form. “POST” in the Method window indicates that the information in the form will be passed to the program processing the form as standard input. Give method=POST

Save your form and test it.

Points to remember:

  • In the form properties, Action=URL of your sendresults.php file and Method=POST
  • Editing the following parameters in sendresults.php file:
    $subject, $emailadd, $url, $req
  • Make sure your host supports PHP