Contact with PHP
For the homepage, there are a few extras, surely belongs in the contact to the most popular. It provides a comfortable contact with the operator of the website and can also apply to other functionalities easily convert.
Here I explain how to use PHP and the function mail () to create a separate form may be. Used to be at least PHP 4, and a mail server, which is however in most hosting services there.
1. Getting Started
At first, we need the HTML based, so the appearance of the form, what the user will see. At first, we need of course a source file that I match the project “contact.php” have mentioned. Then begins the actual Code: this we create a simple text boxes with the <form> day:
<Form action = “contact.php” method = “post”>
Your e-mail address: <br />
<Input type = “text” name = “email” /> <br />
Your message: <br />
<Textarea name = “message” cols = “50″ rows = “5″> </ textarea> </ br>
<Input type = “submit” value = “Submit” />
</ Form>
The first day
<Form action = “contact.php” method = “post”>
is the skeleton of the form that action will be determined which file the data is processed. Here I have logically named “contact.php” is selected, the name of our file. Then, still down method, the way the data are transferred. In PHP there are two options, GET and POST. GET, the public values and stores them in the dynamic URL, so things like index.php? S = 82882 established. However, the POST data is anonymous, and thus a contact for the better version.
Next, we define two fields, the first
<Input type = “text” name = “email” />
is an input field, which we define as text type, so it is writable, then we give him the name “email”. This name is later in the PHP part of relevance. The next day is a textarea tag, basically the same as input type = “text”, but is better suited because it is in the region can define, cols columns and stands for is the width designation, the number of rows rows. When I name “message” is used, because this is the real message will be. Finally, we need a button to the action to start. These take the input type “submit” under value, you have to be marked on the button firmly. At the beginning of the open form tag, we must now include: </ form>
This real structure can be arbitrarily extend important is that the used name-values do not appear twice.
2. PHP in action
Next, we will form with the actual function expand. For that we need in fact only the PHP function mail (), this is defined by the structure of mail ( “recipient address”, “Subject”, “message”, “From:” address “);. Theoretically it would be so rich following code to write to the message given:
<? php
@ Mail ( “deineadresse@abc.de,” “Contact”, $ _POST [ 'message'], “From: $ _POST [email]“);
?>
The code would enter the message that under the variable $ _POST [ 'message'] was defined to the recipients address. As the subject would “contact” to be called and the sender’s address would be the address of the user: $ _POST [email]. The names of the variables have the pattern $ _POST [], because we as a field in the HTML POST method selected. The term within the square brackets is the respective value in the name-tag form, which in our case the values in the
<Textarea>
and
<Input type = “text” />
.
Security
The current problem is that the website operator, even if an e-mail you get, if one or both of these fields left blank. A welcome leak for spammers. For this reason, we use an if-query to test whether the fields are free to use the function! Empty (variable name). This question from whether a value is less than or equal to zero, if none is available, so the e-mail is not sent:
<? php
if (! empty ($ _POST [ 'email']) & &! empty ($ _POST [ 'message'])) (
@ Mail ( “deineadresse@abc.de”, “feedback”, $ _POST [ 'message'], “From: $ _POST [email]“);
)
?>
The if-query begins! Empty ($ _POST [ 'email']) and is & &! (_$[' Empty message ']) expanded. Only then comes the brace and therefore the existing code, thus sending the message. Then after the if-sentence with a further brace closed.
Next, we should ensure that no alien HTML code is typed, not as external files. We use the best function htmlspecialchars ().
<? php
if (! empty ($ _POST [ 'email']) & &! empty ($ _POST [ 'message'])) (
$ email = htmlspecialchars ( “$ _POST [email]“);
@ Mail ( “pdalipi@gmail.com”, “feedback”, $ _POST [ 'message'], “From: $ email”);
)
?>
First we create a new variable $ email, it will use the function htmlspecialchars () is defined, the command worked. In the mail () function so we set the new variable instead of the old one, since they changed the value.
Convenience
On the Web often times it happens that you are somehow verklickt or something wrong. Let’s say you accidentally press the F5 key, then the page reloads and the whole message is gone. Therefore, it is good if you store the values entered. We use the function isset () to query the value and the function echo output to the data. Since this is within the fields to appear, the PHP code into a new definition of value entered values.
<input type = “text” name = “email” value = “<? php
if (isset ($ _POST [ 'email'])) (
echo htmlspecialchars ($ _POST [email] “);
)> “/>
<textarea name = “message” cols = “50″ rows = “5″ value = “<? php
if (isset ($ _POST [ 'message'])) (
echo htmlspecialchars ($ _POST [message] “);
)?> “> </ Textarea>
We now have both a value-tag value is generated in the quotes is now the PHP code, with the function isset () first asks whether a value is contained and then possibly by the function htmlspecialcahrs () from HTML Code-free and echo issues.
3. Tools
The contact would be really ready. To improve, you can, however, one or more additional coding. This leads me to some examples:
Confirmation
Through a simple query if we can spend to the user that his message has been sent, we will modify the existing code around and add another if-set. Here is the original code:
<? php
if (! empty ($ _POST [ 'email']) & &! empty ($ _POST [ 'message'])) (
$ email = htmlspecialchars ( “$ _POST [email]“);
@ Mail ( “pdalipi@gmail.com”, “feedback”, $ _POST [ 'message'], “From: $ email”);
)
?>
Now we set the mail ()-function if another query, the additional spending that the message was sent.
<? php
if (! empty ($ _POST [ 'email']) & &! empty ($ _POST [ 'message'])) (
$ email = htmlspecialchars ( “$ _POST [email]“);
if (@ mail ( “pdalipi@gmail.com”, “feedback”, $ _POST [ 'message'], “From: $ email”)) (echo “The message has been sent”;
) Else (
echo “There was an error”;
)
)
?>
The mail () function is called if-question set, this works, then the text “The message was sent” by echo-output function. If something is not worked, it appears the message “There was an error in the attachment else.
With JavaScript were on these commands, of course, pop-ups that are perhaps more visually hermachen would.
One could query a positive course, a confirmation mail. This one uses the mail () function a second time, but is now the recipient’s address $ _POST [email], and the FROM: address of their own. In addition, you should obviously change the subject and message.
Captcha
For better Spamprotectiontext Captchaabfrage course would also be useful. A detailed description would, however, beyond the scope.

You must be logged in to post a comment.