hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Programming Tutorials : PHP Contact Form
Reply

Programming Tutorials How-Tos related to programming, databases, and the like.
Forum Jump

PHP Contact Form

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 01-19-2006, 07:08 PM
Neoboffin Neoboffin is offline
Junior Guru
 
Join Date: Mar 2005
Location: Derby, UK
Posts: 219

PHP Contact Form


Simple form. Works with register_globals on.

This little script works within your page so you don't need 2 pages to process data, and it won't ruin your layout. Simple, clean and you can add departments by changing the self explanatory array.

I didn't find one like this in this section, so I've posted it just to help others who may need one as I think every host needs a contact page.

All you need to do, is create a page, put the below code where you want the contact form to show, edit $YourEmail and $YourDepartments variables, save the page with a .php extension and your ready to go!

Quote:
<?php
// Configuration Start \\
$YourEmail = "youremail@yourdomain.com";
$YourDepartments = array("Abuse","Domains","General","Other");
// Configuration End \\

if($Submit)
{
if(empty($email) || empty($name) || empty($message))
{
echo "You forgot to enter some required fields. Please go back and try again.";
}
elseif(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $email))
{
echo "The e-mail is not valid. Please go back and try again.";
}
else
{
$Message = "$name has contacted the $Department department. The message is below.\n\n***************\n$message\n***************\n\nIP of sender: $_SERVER[REMOTE_ADDR]\nE-mail of sender: $email";
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($YourEmail, $Department, stripslashes($Message), $headers);
echo "The form has been sent and you shall receive a reply shortly.";
}
}
else
{
echo '<form name="cf" method="post" action="">
<table border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td align="right"><strong>Your Name:</strong></td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td align="right"><strong>Your E-Mail:</strong></td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td align="right"><strong>Department:</strong></td>
<td><select name="Department">';
foreach($YourDepartments as $Department)
{
echo '<option>' . $Department . '</option>';
}
echo '</select></td>
</tr>
<tr>
<td align="right" valign="top"><strong>Message:</strong></td>
<td><textarea name="message" cols="30" rows="6" id="message"></textarea></td>
</tr>
</table>
<p align="center">
<input type="submit" name="Submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</p>
</form>';
}
?>


Last edited by SoftWareRevue; 08-23-2006 at 09:21 AM. Reason: Fixing scrollbar.
Reply With Quote


Sponsored Links
  #2  
Old 04-21-2006, 12:08 PM
boxxy boxxy is offline
Newbie
 
Join Date: Apr 2006
Posts: 11
Is there a demo anywhere?

Reply With Quote
  #3  
Old 07-27-2006, 05:05 PM
Top Nurse Top Nurse is offline
Newbie
 
Join Date: Jul 2006
Posts: 13
Question

First of all thanks to Neoboffin for posting the script.

I have been trying to get this to work on our website put it seems to not work properly and I don't know enough programming to see what is up. I think the problem is with this line = X-Mailer: PHP/' . phpversion();

BTW, is there any way to use this script to utilize SMTP?

Here is the code we used:

Quote:
<?php
// Configuration Start \\
$YourEmail = "contactus@baycitiesna.com";
$YourDepartments = array("Activities","Area Chair","Area Secretary","Convention","External Vice-Chair","Hospitals & Institutions","Internal Vice-Chair","Literature Distribution","Men's Luncheon","Newsletter","Phonelines","Public Information","Punk Show","Rockfest","Treasurer","Website","Women's Luncheon","Women's Retreat","Other");
// Configuration End \\

if($Submit)
{
if(empty($email) || empty($name) || empty($message))
{
echo "You forgot to enter some required fields. Please go back and try again.";
}
elseif(!eregi('^([._a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,3})?)$', $email))
{
echo "The e-mail is not valid. Please go back and try again.";
}
else
{
$Message = "$name has contacted the $Department department. The message is below.\n\n***************\n$message\n***************\n\nIP of sender: $_SERVER[REMOTE_ADDR]\nE-mail of sender: $email";
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($YourEmail, $Department, stripslashes($Message), $headers);
echo "The form has been sent and you shall receive a reply shortly.";
}
}
else
{
echo '<form name="cf" method="post" action="">
<table border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td align="right"><strong>Your Name:</strong></td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td align="right"><strong>Your E-Mail:</strong></td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td align="right"><strong>Department:</strong></td>
<td><select name="Department">';
foreach($YourDepartments as $Department)
{
echo '<option>' . $Department . '</option>';
}
echo '</select></td>
</tr>
<tr>
<td align="right" valign="top"><strong>Message:</strong></td>
<td><textarea name="message" cols="60" rows="10" id="message"></textarea></td>
</tr>
</table>
<p align="center">
<input type="submit" name="Submit" value="Send">
<input type="reset" name="Reset" value="Reset">
</p>
</form>';
}
?>


Last edited by SoftWareRevue; 08-23-2006 at 09:20 AM. Reason: Fixing scrollbar.
Reply With Quote
Sponsored Links
  #4  
Old 07-27-2006, 11:16 PM
Top Nurse Top Nurse is offline
Newbie
 
Join Date: Jul 2006
Posts: 13
It isn't going to work because the function I need will not be provided by my hosting company: register_globals

Reply With Quote
  #5  
Old 07-28-2006, 09:12 AM
arkin arkin is offline
Web Hosting Guru
 
Join Date: Feb 2003
Location: L.A. C.A.
Posts: 334
register_globals is defined in all versions of PHP, whether it is allowed or not.
If however it is not set to on, you can simply define the variables.

Attach to top:
PHP Code:
$email=$_POST['email'];
$name=$_POST['name'];
$message=$_POST['message']; 
Another factor i'd be scared of is injection, be careful.

Reply With Quote
  #6  
Old 07-28-2006, 11:23 PM
horizon horizon is offline
Web Hosting Master
 
Join Date: Mar 2006
Posts: 961
There's also this one for more efficient verification method:

PHP Code:
$email = (isset($_POST['email'])) ? (stripslashes(trim($_POST['email']))) : "";
$name = (isset($_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";
$message = (isset($_POST['message'])) ? strip_tags(trim($_POST['message'])) : ""

Reply With Quote
  #7  
Old 07-29-2006, 12:28 AM
brendandonhu brendandonhu is offline
Web Hosting Master
 
Join Date: Nov 2003
Posts: 682
You might want to do some validation on $Department as well, making it a <select> isn't stopping anyone from putting in any value they like.

Reply With Quote
  #8  
Old 07-29-2006, 01:21 AM
Christian Christian is offline
Community Liaison
 
Join Date: Aug 2005
Posts: 454
Quote:
Originally Posted by arkin
register_globals is defined in all versions of PHP, whether it is allowed or not.
That is until PHP 6 comes out... They finally get rid of them!
http://www.sitepoint.com/blogs/2006/...t-be-in-php-6/

Reply With Quote
  #9  
Old 07-29-2006, 01:23 AM
Christian Christian is offline
Community Liaison
 
Join Date: Aug 2005
Posts: 454
Quote:
Originally Posted by brendandonhu
You might want to do some validation on $Department as well, making it a <select> isn't stopping anyone from putting in any value they like.
Exactly, never trust the user. Make use you are getting the expected input.

Reply With Quote
  #10  
Old 07-29-2006, 09:10 AM
horizon horizon is offline
Web Hosting Master
 
Join Date: Mar 2006
Posts: 961
Replace:

PHP Code:
$email = (isset($_POST['email'])) ? (stripslashes(trim($_POST['email']))) : "";
$name = (isset($_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";
$message = (isset($_POST['message'])) ? strip_tags(trim($_POST['message'])) : ""
with:

PHP Code:
$email = (isset($_POST['email'])) ? (stripslashes(trim($_POST['email']))) : "";
$name = (isset($_POST['name'])) ? (stripslashes(trim($_POST['name']))) : "";
$message = (isset($_POST['message'])) ? strip_tags(trim($_POST['message'])) : "";
$department = (isset($_POST['department'])) ? (stripslashes(trim($_POST['department']))) : NULL
Then,

replace:

PHP Code:
if(empty($email) || empty($name) || empty($message)) 
with:

PHP Code:
if(empty($email) || empty($name) || empty($message) || $department == NULL

Should be all set.

Reply With Quote
  #11  
Old 07-30-2006, 01:01 AM
brendandonhu brendandonhu is offline
Web Hosting Master
 
Join Date: Nov 2003
Posts: 682
They can still set the subject line to whatever they want...

Reply With Quote
  #12  
Old 07-30-2006, 08:56 AM
horizon horizon is offline
Web Hosting Master
 
Join Date: Mar 2006
Posts: 961
Quote:
hey can still set the subject line to whatever they want...

Subject line ? Sorry there but I don't see any variables named anything like subject ...

Reply With Quote
  #13  
Old 07-30-2006, 03:30 PM
brendandonhu brendandonhu is offline
Web Hosting Master
 
Join Date: Nov 2003
Posts: 682
Whatever the variable is called, the 2nd parameter to mail() is the subject. They can POST any value for $Department and it will go directly into mail().


Last edited by brendandonhu; 07-30-2006 at 03:45 PM.
Reply With Quote
  #14  
Old 08-17-2006, 11:51 AM
BurakUeda BurakUeda is offline
Retarded Noodleator
 
Join Date: Oct 2004
Location: Shimonoseki
Posts: 2,100
Email header injection anyone?

Reply With Quote
  #15  
Old 08-17-2006, 02:30 PM
brendandonhu brendandonhu is offline
Web Hosting Master
 
Join Date: Nov 2003
Posts: 682
Quote:
Originally Posted by BurakUeda
Email header injection anyone?
See the post above yours...

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Fahrenheit Marketing to Foot the Bill for 500 Texas Non-Profits' Web Hosting Web Hosting News 2012-12-31 16:22:22
Lead Generation Part 1 – Form Fills Blog 2012-11-16 09:03:48
All New 2013 WHIR Networking Event Schedule Announced Blog 2013-04-15 13:52:59
Monday Demo: Veeam's Backup & Replication Software Blog 2012-02-13 10:56:52
Security Researchers Find Phishing Sites on Google Docs Web Hosting News 2011-05-31 14:32:36


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?