PHP Form Script
Posted: Fri Feb 06, 2009 8:37 am
Hey Guys,
im new here and im kinda stuck on a simple problem.
i need an email form which where a user can select which person they want to email (from drop down box).
Below is the code which i have atm..
any help will be great, and sorry if this is in the wrong section
-DreamOn
im new here and im kinda stuck on a simple problem.
i need an email form which where a user can select which person they want to email (from drop down box).
Below is the code which i have atm..
Code: Select all
<?php
class maxContact {
var $subjectText = "Message From Website Contact Form!";
var $targetEmail = '';
var $mailFormat = "text/plain";
var $fieldList;
var $errorList;
var $email = '';
var $name = '';
var $message = '';
var $additionalText = '';
function maxContact(){
//Extend the list as you want here
$this->fieldList[0]['caption'] = "Mesto";
$this->fieldList[0]['fieldName'] = "mesto";
$this->fieldList[0]['value'] = "";
}
function showForm(){
$message = '';
echo ' <p><form action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo ' <select name="$targetEmail">'
.' <option value="john@example.com" name="john">John</option>'
.' <option value="mark@example.com" name="mark">Mark</option>';
echo ' </select><br />';
echo ' Full Name:<br />'
.' <input type="text" name="name" value="'.$this->name.'" size="40"/><br />';
echo ' Email:<br />'
.' <input type="text" name="email" value="'.$this->email.'" size="50"/><br />';
echo ' Message:<br />'
.' <textarea cols="65" rows="15" name="message">'.$this->message.'</textarea><br />'
.'<input type="submit" name="submitBtn" class="sbtn" value="Send" />';
echo '</form></p>';
}
function sendMail() {
$subject = $this->subjectText;
$from = "From: $this->name <$this->email>\r\nReply-To: $this->email\r\n";
$header = "MIME-Version: 1.0\r\n"."Content-type: $this->mailFormat; charset=utf-8";
$content = $this->message;
if ($this->additionalText != ''){
$content .= "\r\n\r\nAdditional information:\r\n\r\n"
.$this->additionalText;
}
$content = wordwrap($content,70);
@mail($this->targetEmail,$subject,$content,$from.$header);
}
function isValidEmail($email){
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
if (eregi($pattern, $email)){
return true;
}
else {
return false;
}
}
function processForm(){
if (isset($_POST['submitBtn'])){
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
if (!$this->isValidEmail($email)) $this->errorList[] = "Invalid email address!";
if (strlen($name)<2) $this->errorList[] = "Invalid name! It must be at least 2 characters long.";
if (strlen($message)<5) $this->errorList[] = "Invalid message! It must be at least 10 characters long.";
$this->email = $email;
$this->name = $name;
$this->message = htmlspecialchars($message);
foreach ($this->fieldList as $key=>$value) {
if (isset($_POST[$value['fieldName']])) {
$this->fieldList[$key]['value'] = $_POST[$value['fieldName']];
$this->additionalText .= $value['caption'] . " : " . $_POST[$value['fieldName']]."\r\n";
}
}
if (sizeof($this->errorList) > 0){
$this->showErrors();
$this->showForm();
} else {
$this->sendMail();
$this->showInfo();
}
} else {
$this->showForm();
}
}
function showErrors(){
foreach ($this->errorList as $value) {
echo "<p>$value</p>";
}
}
function showInfo(){
echo "<p>Thanks for your message!</p>";
}
}
?>-DreamOn