Adding More Features to Feedback Form

Before we start if you want you can recall the previous parts of this tutorial:

How to Create Feedback Form in PHP - Part I
How to Create Feedback Form in PHP - Part II

Now we can also improve the script and feedback form in the following ways:

1. Add support for your visitor’s name. Otherwise when you reply, you would not be able to address him or her by name. You will of course need to code your script so that it includes those fields. For example, if your name field is called “name”, you might want to modify your call to the mail function as follows:

1
2
mail( "yourname@example.com", "Feedback Form Results",
  $message, "From: $name <$email>" );

2. Likewise if you include other fields like: “How did you hear of this website?”, you might want to encode the results as in the following example. Here I assume that the answer is given in the field named “whosentyou”.

1
2
3
mail( "yourname@example.com", "Feedback Form Results",
  "$message\nHow did you hear of this website? $whosentyou\n",
  "From: $name <$email>" );

The “\n” inserts a new line character into the message, causing the line “How did you hear of this website? (etc)” to begin on a new line.

3. Improve the formatting on the feedback form itself. Put it in a table, or use Cascading Style Sheets (if you want you can also go through Learn CSS from Scratch (Cascading Style Sheets) - Part I to make feedback form more better) , to align the various form elements; expand the size of the various input boxes, or otherwise beautify the appearance of the form. It currently looks quite ugly, although functional. However you get the base and above all you learnt it.

 
Make it Global Feedback Form

It is possible to make your PHP script sendmail.php, global and it can serve both frontend and backend, the form web page, the email sending script (which is what it is now), as well as the “Thank You” page. The visitor who loads sendmail.php will immediately see your feedback form, and “sendmail.php” will appear in his browser’s location bar. After he sends his feedback, he receives the “Thank You” page, but the address bar in his browser still shows “sendmail.php”.

This is trivial to implement. Now remove the folowing line from sendmail.php

1
  header( "Location: http://www.example.com/feedback.html" );

and add the feedback.html code just after this statement:

1
  if (!isset($_REQUEST['email'])) {

Now lets see the complete modified code:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
 
  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;
 
  if (!isset($_REQUEST['email'])) {
?>    
 
    <html>
      <head>
      <title>HeyGB.com - Feedback Form</title>
      </head>  
      <body>
 
      <form method="post" action="">
        Email: <input name="email" type="text" /><br />
        Message:<br />
        <textarea name="message" rows="15" cols="40">
        </textarea><br />
        <input type="submit" />
      </form>
 
      </body>
    </html>
 
<?php  
  }
  elseif (empty($email) || empty($message)) {
 
    header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Cache-Control: no-cache, must-revalidate" );
    header( "Pragma: no-cache" );  
?>
 
    <html>
    <head><title>Error</title></head>
    <body>
    <h1>Error</h1>
    <p>
    Oops, it appears you forgot to enter either your
    email address or your message. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>
 
<?php
  }
  else {
    mail( "yourname@example.com", "Feedback Form Results",
          $message, "From: $email" );
    header( "Location: http://www.example.com/thankyou.html" );
  }
 
?>

Now you can directly run your sendmail.php file (http://www.example.com/sendmail.php) and you don’t need to run feedback.html file because your sendmail.php file contains all the code of feedback.html and also contains custom HTML error message. Now you have one global file. If you want you also write Thanks message of thankyou.html file by removing this line in senmail.php:

1
  header( "Location: http://www.example.com/thankyou.html" );

and write the code of thankyou.html which we covered in How to Create Feedback Form in PHP - Part I. Here is the modified code:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
 
  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;
 
  if (!isset($_REQUEST['email'])) {
?>    
 
    <html>
      <head>
      <title>HeyGB.com - Feedback Form</title>
      </head>  
      <body>
 
      <form method="post" action="">
        Email: <input name="email" type="text" /><br />
        Message:<br />
        <textarea name="message" rows="15" cols="40">
        </textarea><br />
        <input type="submit" />
      </form>
 
      </body>
    </html>
 
<?php  
  }
  elseif (empty($email) || empty($message)) {
 
    header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
    header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
    header( "Cache-Control: no-cache, must-revalidate" );
    header( "Pragma: no-cache" );  
?>
 
    <html>
    <head><title>Error</title></head>
    <body>
    <h1>Error</h1>
    <p>
    Oops, it appears you forgot to enter either your
    email address or your message. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>
 
<?php
  }
  else {
    mail( "yourname@example.com", "Feedback Form Results",
          $message, "From: $email" );
 
?>
 
 <html>
  <head>
  <title>HeyGB.com - Feedback Form</title>
  </head>
 
  <body>
    <center>Thanks you for Contacting us ! Soon we will get back to you.</center>
  </body>
 </html>
 
<?php          
  }
 
?>

Isn’t it great :)