Contact Form for your website : If you are game to try

Hopefully this will be of some help to you. It is a php script that I wrote for the Contact Page of my free "phpSimpleSite" which can be downloaded here:

http://www.hotscripts.com/Detailed/70795.html

----

Anyway - it will be complex to extract it from the phpSimpleSite layout without a fair bit of coding knowledge.

What I just did was take out the parts and put them into a one page script that contains the 2 different elements - - - the entry Form parts at the bottom - - - and the Action taken at the top when the Submit button is clicked.

Nothing is formatted - so you would have to figure out how to make it look good and fit into your website, but you can test the script on it's own on your server and see that it works well and will send you an email (as if it were someone sending a message from your website. It checks to make sure the fields are filled in and that the email follows the "hasfhjas@hasfhjas.jad" format. IF it doesn't an error page is displayed and they are able to go back and make the changes. It also collects a couple of pieces of info about the computer, browser and server the request was made from - - - that are tacked on to the email you recieve.

To test it, copy and paste this script into Notepad or some other text editor and change the $email_address = "you@yoursite.com"; to reflect your email address and save the script as something like "contact.php". Upload that script to your server and type in your website url plus "/contact.php" if that is the name you use - - - and you should see the contact form.  Try leaving empty fields on the form, Submit and see what happens. Try putting an inproper email format in, Submit and see what happens. Then enter all the fields properly and send your email. Check your email to see if you recieved it. I'd test these things first before trying to fit it in to your website. get used to how it works first:


Code:
<?php

$email_address 
"you@yoursite.com";


if (
$_POST['submit']){
$required_fields explode(","$_REQUEST['required_fields']);
for($i 0$i count($required_fields); $i++) {
if($$required_fields[$i] == "") {
echo "<html><head><title></title></head><body>The required field $required_fields[$i] is empty!";
echo "<br><br><a href=\"javascript:history.back(1)\">Go Back</a></body></html>";
exit;
}
}
$check_email_address $_REQUEST['check_email_address'];
if($check_email_address == "yes") {
if(!(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$"$email))) {
echo "<html><head><title></title></head><body>The email address: $email is not valid!";
echo "<br><br><a href=\"javascript:history.back(1)\">Go Back</a></body></html>";
exit;
}
}
$fields array_keys($HTTP_POST_VARS);
for($i 0$i count($fields); $i++) {
$actual_var $fields[$i];
$actual_val stripslashes($HTTP_POST_VARS[$actual_var]);
$inside_mail.= "$actual_var: $actual_val\n";
}
$cname gethostbyaddr($REMOTE_ADDR);
$inside_mail.=
"
-----------------------------------------------------------------------
 SENDER INFO:
 IP: $REMOTE_ADDR
 Computer Name: $cname
 Browser Type: $HTTP_USER_AGENT
 Page Referer: $HTTP_REFERER
-----------------------------------------------------------------------
"
;
mail($email_address$subject$inside_mail"From: $name <$email>");
echo "<html><head><title></title></head><body>Thank you $name for posting a message - I check my messages regularly and will respond as soon as possible.";
echo "<br><br><a href=\"javascript:history.back(1)\">Go Back</a></body></html>";
exit;
}
?>



<!-- ************************************************************************************************************ -->
<!--          HERE IS WHERE THE PAGE CONTENT AND ENTRY FORM START - HTML SO CAN EASILY BE EDITED         -->

<html>
<head>
<title>Contact Form</title>
</head>
<body>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">

<input type="hidden" name="required_fields" value="subject,name,email,message">
<input type="hidden" name="subject" value="Website Message">
<input type="hidden" name="check_email_address" value="yes">

Name: <input type="text" name="name" size="30"><br />
Email: <input type="text" name="email" size="30"><br />
Message: <textarea wrap="virtual" rows="10" cols="50" name="message"></textarea><br />
<input type="reset" value="Reset">&nbsp;&nbsp;<input type="submit" name="submit" value="Send Message">

</form>
</body>
</html>





« Last Edit: June 28, 2007, 03:19:05 PM by robertwatcher »
Logged