New Members

 Herr. of Design

 To ask for aid


Processing a form with PHP.
Part 2: We begin to program!
By: Falcon.

, Since we have cakes and coffee next to the computer, we are going very well to take off the shoes to put to us but comfortable since when finishing this second part we must have a form with basic functionality in our website with VPS web hosting that we have, and we proud to said that we are supported by our vps hosting company to make all this happen.

As diria Cantinflas: we begin by the principle.

As it mentioned in the first part of this lesson, PHP in a hypertext preprocessor that the servant interprets before sending the document to the visitor.
The PHP tuna is that it is possible to be mixed with simple HTML. This means that it can put code PHP in any place of his document HTML as long as the document has the extension .php, for example: documento.php

But that separates to code PHP of the rest of the document? Once the servant “realizes? of which in the document there is code PHP that needs to be interpreted, it begins to look for in the same labels as these:

<? does php go its code PHP here? >

That is to say: everything what this content between <? php and? > is code PHP.

We enter matter.

We take our text editor and we are going to create a called file enviar_email.php.

In the same we are going to open a label PHP:

<? php

First that there is to do it is to catch the variables that arrived from contactenos.html with the method POST explained in part 1. If it remembers well, they are 3: name, email and message.

As comentabamos in it would prioritize it part, PHP has a specific form to define as they are the variables for his use. This form is by means of a symbol of weights ($) before the name of the variable.

$elnombre = $_POST [‘name’];
$elemail = $_POST [‘email’];
$elmensaje = $_POST [‘message’];

To see, to see, but slowly!

OK, ok, we are going to detach the previous thing:
$elnombre, $elemail and $elmensaje are the names of the variables that we are going to use in this script and are going to take the value from the variables that arrived from the form.

$_POST [?] Vamos to say that it is the form that PHP must to know that it must look for between the variables that arrived with the method POST. In this case we are interested in three variables:

$_POST [‘name’]
$_POST [‘email’]
$_POST [‘message’]

An example:

$elnombre = $_POST [‘name’]

In colloquial language this entenderia thus: The variable elnombre has the value of the variable name that arrived at this script with the method POST.

There is only a part that we needed, and is semicolon (;) at the end of each line. PHP needs to know where each step finishes than it needs to do, we obtained this semicolon using at the end of each line.

We repeat as quedaria:

$elnombre = $_POST [‘name’];
$elemail = $_POST [‘email’];
$elmensaje = $_POST [‘message’];

At this moment PHP has in their memory 3 variables that can use and these are: $elnombre, $elemail and $elmensaje. These three variables took their value from the respective fields that the visitor filled in the form Web.

Before sending the e-mail we needed to define other variables.
That serious of an e-mail without a subject, truth? We are going to define the subject:

$elasunto = “finishes to Me contacting in my website! ?;

And where it is going to go the e-mail:

$adondeva = “;

It does not forget to replace this with his own email address.

Another variable that is necessary to define before sending the e-mail is the headed ones of the mail. So that an e-mail can arrive at its destiny is necessary that it has some headed, of another form a series of rare things can happen.

$encabezados. = “From: $elnombre<$elemail> \ n?;
$encabezados. = “X-Sender: <$elemail> \ n?;
$encabezados. = “X-Mailer: PHP \ n?;
$encabezados. = “Return-Path: <$elemail> \ n?;

But slowly? ok, we are going to detach it.

First that note is here that we are defining several variables with the same name… as can be this? If one pays attention, after the name of each variable there is a point before the equal sign. This point means that there is “to concatenate? all the variables that have the same name. To concatenate in a term that they like to use the programmers because it is heard very “Nice? but does not mean another thing but that “to join?.
This is only made so that script is but easy to read since of it not to have done thus, the variable $encabezados deberia to be defined thus:

$encabezados = “From: $elnombre<$elemail> \ to nX-Sender: <$elemail> \ to nX-Mailer: PHP \ nReturn-Path: <$elemail> \ n?;

It is so great that not even quota in a line, but for practical aims is the same that but confused and only difficult one to read.

Another thing that jumps at sight is that we are using variable defined before.
For example in the headed From we are mentioning the variables $elnombre and $elemail. This becomes because in this heading it must go the name and the eletrónico email address of whom the e-mail sends and the unique way that we must to know as they are this data is taking them from the variables $elnombre and $elemail that arrived to us from the form.
PHP will replace “$elnombre? and “$elemail? and so the visitor has filled in the field names and email of the form. There is an example of this but down.

There are some already will have noticed another rare thing in the segment of code of above, and this is \ n almost at the end of each line.
We remember that an e-mail is not another thing that a text file that travels through cyberspace. “Backslash? followed of a “n? says to him to PHP that there is to create new line since this is necessary so that the mail programs interpret the headed ones correctly.

We are going to put an example: We suppose that Juan Perez who has the e-mail: wishes us to send an e-mail. Full the form and when arriving at this part the headed ones must be read thus:

From: Juan Perez < >
X-Sender: < >
X-Mailer: PHP
Return-Path: < >

If nonhicieramos that were new line between the headed ones, these are leerian thus:

From: Juan Perez < to >X-Sender: < to >X-Mailer: PHPReturn-Path: < >

Some servants or programs of mail do not understand this, they look for each headed in a new line, of there the importance of defining each headed in a new line.

A small count.

We are going to see as script goes ours until this moment:

<? php
$elnombre = $_POST [‘name’];
$elemail = $_POST [‘email’];
$elmensaje = $_POST [‘message’];

$elasunto = “finishes to Me contacting in my website! ?;

$adondeva = “;

$encabezados. = “From: $elemail<$elemail> \ n?;
$encabezados. = “X-Sender: <$elemail> \ n?;
$encabezados. = “X-Mailer: PHP \ n?;
$encabezados. = “Return-Path: <$elemail> \ n?;

Seen is tuna is not thus? we are going to continue:

We are going to format the message so that also tuna is seen:

$mensaje. = “Here this what arrived: \ n \ n?;
$mensaje. = “$elnombre with the direction: $elemail \ n?;
$mensaje. = “It wrote: \ n$elmensaje \ n?;

We are already ready to send it!


Before sending our message it is necessary to exlicar what they are the functions in PHP. We are going to say that the functions in PHP are not but that “action that PHP knows since to do? There are hundreds of functions, one otherwise used is the function mail (). We are going to explain it:

Podria not to be but easy to send a mail, the unique thing that must do is to mention the function mail () and ready. Within the parentheses several data must go and these are: to where the mail goes, the subject, the message and the headed ones. All separated one by commas:

mail (to where it goes, the subject, the message, the headed ones);

We are going to fill to the function mail with the data of our own script:

mail ($adondeva, $elasunto, $mensaje, $encabezados);

Ready! one finishes sending an e-mail with the data that the visitor filled in the form! We only need to close label PHP:


Falcon hears: when running script in the servant if it sends the e-mail but only puts a white screen to me…

We are going to give the utlimos adjustments him and upgrade our hosting to the next level using unlimited hosting plans but that is going to be in:

Part 3: The last details -->
<-- Part 1


Web Hosting | Packages | Frequent questions | Contact to us | Terms and Conditions | It orders Now