Host Matrix Forums  

Go Back   Host Matrix Forums > Tutorials & Articles > PHP Tutorials

Reply
 
Thread Tools Display Modes
Old 03-14-2005, 10:53 PM   #1
mck9235
Moderator
 
mck9235's Avatar
 
Join Date: Oct 2004
Location: Maryland
Posts: 1,156
mck9235 Just Getting Started (Qualifies for HM Basic)
Send a message via MSN to mck9235
Feedback form! :-)

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:
Code:
<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:
Code:
<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:
Code:
<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:
Code:
<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 Code:
<?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)/","&nbsp;<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 Code:
<?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.
__________________

| - FAQ - | - Request Format - | - HM Packages - | - ToS - | - AuP - |



| - I'm Only Human! - | - TM-Software - | - Kingdoms of Battle - |
mck9235 is offline   Reply With Quote
Old 03-15-2005, 03:13 AM   #2
Zalt4n
 
Join Date: Feb 2005
Location: New Zealand
Posts: 86
Zalt4n Just Getting Started (Qualifies for HM Basic)
Send a message via MSN to Zalt4n
Quote:
Originally Posted by mck9235
Code:
if(ard[0] .length < 1 ) fail("User address absent.");
That "ard" should be "adr".

Also, what exactly is the pt_register() function for/what does it do?
Zalt4n is offline   Reply With Quote
Old 03-15-2005, 10:01 PM   #3
mck9235
Moderator
 
mck9235's Avatar
 
Join Date: Oct 2004
Location: Maryland
Posts: 1,156
mck9235 Just Getting Started (Qualifies for HM Basic)
Send a message via MSN to mck9235
Ah thanks for catching that mistake.

pt_register() registers the thingys, makes them nifty little variables, instead of using $_POST. However, $_POST would work, I tried both.
__________________

| - FAQ - | - Request Format - | - HM Packages - | - ToS - | - AuP - |



| - I'm Only Human! - | - TM-Software - | - Kingdoms of Battle - |
mck9235 is offline   Reply With Quote
Old 03-16-2005, 02:52 AM   #4
Zalt4n
 
Join Date: Feb 2005
Location: New Zealand
Posts: 86
Zalt4n Just Getting Started (Qualifies for HM Basic)
Send a message via MSN to Zalt4n
Are there any advantages to this over $_POST, or other methods of retrieving... whatever they're called?
Zalt4n is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Feedback Form... mck9235 P. H .P 6 03-14-2005 12:08 PM


All times are GMT. The time now is 08:31 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright hostmatrix.org 2004- 2008