Thursday 5 December 2013

How to Create a Contact Form for Your Website in an Easy Way : A Set of Step by Step Tutorials Using HTML5, CSS3 and PHP (3)

(For the latest PDF files Merger Software please have a look at the top of the left margin, inside the red box.)
Continues From Part 2.

How to dispatch message of the contact form?

To be able to receive messages of visitors your contact form should become live with its required "action." Note that your "action" was empty.

1
<form method="post" action="">

Your form page and your "thanks" page are said to be "client-side" visible to clients. You need your "server-side" PHP page visible to you to put on your web-server. This will forward messages from visitor to a known place of yours, usually a mailbox of yours, created for this purpose or else of your daily usage. If you create it purpose built then could be easily portable when you change your hosting service. You can keep things organised and separate from your other administrative chores. First the version without loop-back to the visitor.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
    $Name = Trim(stripslashes($_POST['Name'])); 
    $City = Trim(stripslashes($_POST['City'])); 
    //$Email = Trim(stripslashes($_POST['Email'])); 
    $Email = "this.visitor@googlemail.com";
    $Subject = Trim(stripslashes($_POST['Subject']));
    $Message = Trim(stripslashes($_POST['Message']));
    //Next send contact to website admin.
    $EmailToAdmin = "carlo.dj@messiahpsychoanalyst.org"; //Put your website admin email here. Don't forget quotes. 
    // body of the email your own admin receives
    $Body = "RE: Contact from visitors";
    $Body .= "\n";
    $Body .= "Name: ";
    $Body .= $Name;
    $Body .= "\n";
    $Body .= "City: ";
    $Body .= $City;
    $Body .= "\n";
    $Body .= "Subject: ";
    $Body .= $Subject;
    $Body .= "\n";
    $Body .= "Email: ";
    $Body .= $Email;
    $Body .= "\n";
    $Body .= "Message: ";
    $Body .= "\n";
    $Body .= $Message;
    $Body .= "\n";
    // send email to admin
    $posted = mail($EmailToAdmin, $Subject, $Body, "From: <$Email>");
    // redirect to thanks page 
    if ($posted){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=demoThanks.php\">";
    }
    else{
      //do nothing for now
    }
?>

Line (9), carlo.dj AT messiahpsychonalyst is my admin who receives messages for my website. I have commented out sender's email place-holder at line (4) to prevent bots and spammers attack my site before I put enough robust barriers on their way. Later I will un-comment that line. In place of that I put email of one old colleague I know (with his permission) at line (5). After finishing the design I remove it. I save this file as a PHP file (I have saved it as demoPoster.php) and upload it to my website.

Note that now I have three PHP file for the contact page in my website.
  • First, demoMain.php which is my original contact page. Now it is pure HTML, but later many PHP parts will be added to it.
  • Second file is the "thanks" file, demoThanks.php. This is also pure HTML saved as a PHP file.
  • Third, is all PHP file in the server-side, demoPoster.php
It is better to upload all of them to the "root" directory of website for the ease of path-finding.

In the next stage,  I change my contact  form page from this,

1
<form method="post" action="">

to this one,

1
<form method="post" action="demoPoster.php">

I test it with filling all fields, except the email field. There is no need and no use of filling that for now, as I have locked it by putting line (5)  in the uploaded demoPoster.php. My admin has received the message and its snapshot is here.

 
So far so good. It works perfectly. I could use it if it was not for the spamming bots.
One of the best filters available is the free open source and flexible reCaptcha filter that tests the visitor for being human rather than a software robot flooding crawler. I should add that to my page to barricade my site. It is in the next post. My other tasks are preventing empty fields and fake emails.
Download PHP code in Zip format, here. Open code as text in browser, here.

Download this tutorial as PDF format here
Download part 1 to part 3 as PDF format here

No comments:

Post a Comment