
01-19-2006, 07:08 PM
|
|
Junior Guru
|
|
Join Date: Mar 2005
Location: Derby, UK
Posts: 219
|
|
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.
|

04-21-2006, 12:08 PM
|
|
Newbie
|
|
Join Date: Apr 2006
Posts: 11
|
|
Is there a demo anywhere?
|

07-27-2006, 05:05 PM
|
|
Newbie
|
|
Join Date: Jul 2006
Posts: 13
|
|
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.
|

07-27-2006, 11:16 PM
|
|
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 
|

07-28-2006, 09:12 AM
|
|
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.
|

07-28-2006, 11:23 PM
|
|
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'])) : "";
|

07-29-2006, 12:28 AM
|
|
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.
|

07-29-2006, 01:21 AM
|
|
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/
|

07-29-2006, 01:23 AM
|
|
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. 
|

07-29-2006, 09:10 AM
|
|
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. 
|

07-30-2006, 01:01 AM
|
|
Web Hosting Master
|
|
Join Date: Nov 2003
Posts: 682
|
|
They can still set the subject line to whatever they want...
|

07-30-2006, 08:56 AM
|
|
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 ...
|

07-30-2006, 03:30 PM
|
|
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.
|

08-17-2006, 11:51 AM
|
|
Retarded Noodleator
|
|
Join Date: Oct 2004
Location: Shimonoseki
Posts: 2,100
|
|
Email header injection anyone?
|

08-17-2006, 02:30 PM
|
|
Web Hosting Master
|
|
Join Date: Nov 2003
Posts: 682
|
|
Quote:
|
Originally Posted by BurakUeda
Email header injection anyone?
|
See the post above yours...
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| 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
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|