mck9235
03-14-2005, 10:53 PM
In this tutorial I will show you how to create a feedback form which mails the users results to you.
The best part about this is that it seems as if the user is sending the mail, his/her name and E-mail
appears in the from and reply line, and you can customize your subjects. Nifty, eh?
1. Get the form ready:
Well first we're going to need a form:
<i>* denotes a required field</i><br /><br />
<form enctype='multipart/form-data' action='feedback.php' method='post' name='fback'>
<b>*</b>Name: <input type="text" name="Name"><br /><br />
<b>*</b>E-Mail Address: <input type="text" name="Email"><br /><br />
Telephone Number: <input type="text" name="Telephone"><br /><br />
Subject: <select name="Subject">
<option>Grapes</option>
<option>Bannanas</option>
<option>Apples</option>
<option>Peaches</option>
<option>Pears</option>
<option>Plums</option>
</select><br /><Br />
<b>*</b>Comments: <br /><textarea name="Comments" cols="50" rows="5">Enter your comments here...</textarea><br /><br />
<input type="submit" onClick="javascript:send_if_valid()" value="Submit">
</form>
As you can see I ask for name, E-mail, phone, subject, and comments. All but telephone are required.
For the sunject I used a drop down menu, to use a text input put this in:
<input type="text" name="Subject">
Just edit out everything from <select... to the ending select tag (</select>).
To add more options to the drop-down hence adding more subject choices add this:
<option>Subject value...</option>
Put that under the last option tag, or anywhere between the select tags.
Also heres a small JavaScript E-mail address validatior:
<script type="text/javascript">
//start validation
function send_if_valid()
{
if( document.fback.Email.value.indexOf("@")== -1 )
fail("No '@' in address");
else
{
var adr = document.fback.Email.value.split("@");
if(adr[0] .length < 1 ) fail("User address absent.");
else if(adr[1] .indexOf(".")== -1) fail("No dot");
else if(adr[1] .length < 3) fail("Domain incorrect");
else document.fback.submit();
}
}
//dispaly errors
function fail(msg)
{ window.alert("E-mail address error:\n" +msg); }
</script>
It's already set to validate on submit if you choose to use it. :)
Save as feedbackform.html
Not the real work, the PHP:
<?php
include("global.inc.php");
$errors=0;
$error="We're sorrry, we encountered some errors when processing your form, please go back and try again.<ul>";
pt_register('POST','Name');
pt_register('POST','Email');
pt_register('POST','Telephone');
pt_register('POST','Subject');
pt_register('POST','Comments');
$Comments=preg_replace("/(\015\012)|(\015)|(\012)/"," <br />", $Comments);
/* begin general error checking */
if($Telephone=="") // checks to see if there is a telephone number
{ //if there isnt we change the value to below, just to show there is none in the message
$Telephone = "($Name chose not to submit a telephone number.)";
}
if($Name=="") // checks to see if there is a name
{
$errors=1;
$error.="<li>You did not fill in the name field and it is required.";
}
if($Email=="") // checks to see if there is an E-mail
{
$errors=1;
$error.="<li>You did not fill in the E-mail address field and it is required.";
}
if($Comments=="") // checks to see if there are comments
{
$errors=1;
$error.="<li>You did not fill in the comment box and it is required.";
}
if($errors==1) echo $error; // echoes the errors if there are any
else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Name: ".$Name."
Email: ".$Email."
Telephone: ".$Telephone."
Subject: ".$Subject."
Comments: ".$Comments."
";
$message = stripslashes($message);
mail("mck9235@gmail.com",$Subject,$message,"From: $Name <$Email>");
?>
<!-- redirect user to the thanks page -->
<!-- /*
<script type="text/javascript">
window.location="location of ty page";
</script> */
-->
<!-- Do not change anything below this line -->
<?php
}
?>
As you see this requires a file called global.inc.php
This is needed as the file is based with some functions from that. We'll make it later.
Now, theres really nothing to edit other than the message if you want to, and the E-mail address it goes to
which is in the mail() function.
Basically the if's just check to make sure fields aren't null and if they are it will produce some errors.
Save as feedback.php
Now for global.inc.php
<?php
function pt_register()
{
$num_args = func_num_args();
$vars = array();
if ($num_args >= 2) {
$method = strtoupper(func_get_arg(0));
if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) {
die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV');
}
$varname = "HTTP_{$method}_VARS";
global ${$varname};
for ($i = 1; $i < $num_args; $i++) {
$parameter = func_get_arg($i);
if (isset(${$varname}[$parameter])) {
global $$parameter;
$$parameter = ${$varname}[$parameter];
}
}
} else {
die('You must specify at least two arguments');
}
}
?>
Save as global.inc.php and keep it in the same directory as the other two files.
Upload and acess feedbackform.html to use it! :) Enjoy. :-)
If I did anything wrong please tell me. :)
The best part about this is that it seems as if the user is sending the mail, his/her name and E-mail
appears in the from and reply line, and you can customize your subjects. Nifty, eh?
1. Get the form ready:
Well first we're going to need a form:
<i>* denotes a required field</i><br /><br />
<form enctype='multipart/form-data' action='feedback.php' method='post' name='fback'>
<b>*</b>Name: <input type="text" name="Name"><br /><br />
<b>*</b>E-Mail Address: <input type="text" name="Email"><br /><br />
Telephone Number: <input type="text" name="Telephone"><br /><br />
Subject: <select name="Subject">
<option>Grapes</option>
<option>Bannanas</option>
<option>Apples</option>
<option>Peaches</option>
<option>Pears</option>
<option>Plums</option>
</select><br /><Br />
<b>*</b>Comments: <br /><textarea name="Comments" cols="50" rows="5">Enter your comments here...</textarea><br /><br />
<input type="submit" onClick="javascript:send_if_valid()" value="Submit">
</form>
As you can see I ask for name, E-mail, phone, subject, and comments. All but telephone are required.
For the sunject I used a drop down menu, to use a text input put this in:
<input type="text" name="Subject">
Just edit out everything from <select... to the ending select tag (</select>).
To add more options to the drop-down hence adding more subject choices add this:
<option>Subject value...</option>
Put that under the last option tag, or anywhere between the select tags.
Also heres a small JavaScript E-mail address validatior:
<script type="text/javascript">
//start validation
function send_if_valid()
{
if( document.fback.Email.value.indexOf("@")== -1 )
fail("No '@' in address");
else
{
var adr = document.fback.Email.value.split("@");
if(adr[0] .length < 1 ) fail("User address absent.");
else if(adr[1] .indexOf(".")== -1) fail("No dot");
else if(adr[1] .length < 3) fail("Domain incorrect");
else document.fback.submit();
}
}
//dispaly errors
function fail(msg)
{ window.alert("E-mail address error:\n" +msg); }
</script>
It's already set to validate on submit if you choose to use it. :)
Save as feedbackform.html
Not the real work, the PHP:
<?php
include("global.inc.php");
$errors=0;
$error="We're sorrry, we encountered some errors when processing your form, please go back and try again.<ul>";
pt_register('POST','Name');
pt_register('POST','Email');
pt_register('POST','Telephone');
pt_register('POST','Subject');
pt_register('POST','Comments');
$Comments=preg_replace("/(\015\012)|(\015)|(\012)/"," <br />", $Comments);
/* begin general error checking */
if($Telephone=="") // checks to see if there is a telephone number
{ //if there isnt we change the value to below, just to show there is none in the message
$Telephone = "($Name chose not to submit a telephone number.)";
}
if($Name=="") // checks to see if there is a name
{
$errors=1;
$error.="<li>You did not fill in the name field and it is required.";
}
if($Email=="") // checks to see if there is an E-mail
{
$errors=1;
$error.="<li>You did not fill in the E-mail address field and it is required.";
}
if($Comments=="") // checks to see if there are comments
{
$errors=1;
$error.="<li>You did not fill in the comment box and it is required.";
}
if($errors==1) echo $error; // echoes the errors if there are any
else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
$message="Name: ".$Name."
Email: ".$Email."
Telephone: ".$Telephone."
Subject: ".$Subject."
Comments: ".$Comments."
";
$message = stripslashes($message);
mail("mck9235@gmail.com",$Subject,$message,"From: $Name <$Email>");
?>
<!-- redirect user to the thanks page -->
<!-- /*
<script type="text/javascript">
window.location="location of ty page";
</script> */
-->
<!-- Do not change anything below this line -->
<?php
}
?>
As you see this requires a file called global.inc.php
This is needed as the file is based with some functions from that. We'll make it later.
Now, theres really nothing to edit other than the message if you want to, and the E-mail address it goes to
which is in the mail() function.
Basically the if's just check to make sure fields aren't null and if they are it will produce some errors.
Save as feedback.php
Now for global.inc.php
<?php
function pt_register()
{
$num_args = func_num_args();
$vars = array();
if ($num_args >= 2) {
$method = strtoupper(func_get_arg(0));
if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) {
die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV');
}
$varname = "HTTP_{$method}_VARS";
global ${$varname};
for ($i = 1; $i < $num_args; $i++) {
$parameter = func_get_arg($i);
if (isset(${$varname}[$parameter])) {
global $$parameter;
$$parameter = ${$varname}[$parameter];
}
}
} else {
die('You must specify at least two arguments');
}
}
?>
Save as global.inc.php and keep it in the same directory as the other two files.
Upload and acess feedbackform.html to use it! :) Enjoy. :-)
If I did anything wrong please tell me. :)