Sending Form Data via PHP

This article gives an example of how form input can be sent by email using the PHP programming language.

Please note: Basic knowledge of HTML and the PHP programming language is recommended for implementing the steps described here. For general information on creating forms in HTML, see this w3schools page. More information about processing form data with PHP can be found on php.net.

Preface

In the example presented here, the data is copied from an HTML form. The script prepares them and sends them to a given email address using the mail() function. A thank you page will then be displayed.

 

The structure of the thank you page and the email are not controlled in the script itself, but via the following configuration files:

 

  • The response.htt file describes the structure of the thank you page.
  • The mail.htt file specifies the format of the email

The HTML form

The example form contains a multiline input field and a submit button. The HTML code looks like this:

<form action="formmail.php" method="POST">
<input type="hidden" name="subject" value="Kontaktformular">
<input type="hidden" name="access" value="irregeheim">
<textarea name="Eingabe" cols="60" rows="20" wrap="PHYSICAL" id="Eingabe"></textarea>
<input type="submit" value="Abschicken">
</form>

The hidden field subject defines the subject of the email (line 2).

Please note: To prevent misuse of the script for spam purposes, the form contains an additional, hidden form field with the name access (line 3). The PHP script will only send the email if this field is passed!

The Thank You page

To simplify maintenance, the thank you page is not part of the PHP script, but is defined in the separate response.htt file. This contains the page content in HTML format and can look like this, for example:

<h1>Danke!</h1>
Ihre Eingabe <Eingabe> wurde an uns verschickt.

The special feature is that you can access the previously entered form fields directly. In this example, we have inserted the contents of the'Input' field by changing the field name to <> Included. The script recognizes this and automatically replaces the code with the content of the input field.

The email format

The appearance of the email is also controlled by a separate file, in this example mail.htt. This determines the further content of the email.

The following example shows how the file must look if it is to contain an introductory sentence and the contents of the form field:

Hallo, aus dem Kontaktformular kam folgende Eingabe:

<Eingabe>

Here, too, stands <Eingabe>for the content of the respective form field</Eingabe>.

If someone enters hello in the corresponding form on the website, the email sent by the PHP script looks like this:

"Hello, from the contact form came the following entry:

Hello"

The PHP script

The script should ensure the following: It accepts the form data, displays a thank you page and sends it to the email address specified in the script.

<?php
$recipient = "mail@1und1hilfe.de";
$fmtResponse= implode("", file("response.htt"));
$fmtMail= implode("", file("mail.htt"));
foreach($_POST as $key=> $val) {
  $fmtResponse= str_replace("<$key>", $val, $fmtResponse);
  $fmtMail= str_replace("<$key>", $val, $fmtMail);
}
if ($_POST["access"] == "irregeheim") {
  mail($recipient, $_POST["subject"], $fmtMail);
}
echo $fmtResponse;
?>

In line 2 you specify the email address to which the form data is sent. In this example, the mail@1und1hilfe.de.

Line 3 reads the content of the thank you page from the response.htt file. It is therefore necessary that you create them before the first test run.

In line 4 of the PHP script the file mail.htt is read.

Setup and test run

Insert the code for the HTML form into your page and make sure that the following files are
in the same directory:

  • formmail.php: the first script example on this page.
  • response.htt: the thank you page.
  • mail.htt: the file that defines the email format.

Then go to your page and fill in the form. You should now receive the data by email.

troubleshooting

If the thank you page is not displayed, or the email has no content, please check whether you have correctly named the files resonse.htt or mail.htt. It is also necessary that the files are in the same directory.