webForumDet fria alternativet

Kontaktformulär i en klass

PHP

0 svar · 16 223 visningar · startad av Melea

Medlem sedan juli 20033 147 inlägg
Frågan#1

Satt och hjälpte en vän med ett kontaktformulär idag och tänkte dela med mig vad jag kom fram till.

<?
unset($_SESSION['error']);

class contact {

	public function __construct() {
	}
	
	public function sendEmail($from_name, $from_email, $message) {
		
		$this->from_name = $from_name;
		$this->from_email = $from_email;
		$this->message = $message;
		$this->to = 'info@yoursite.com'; // your email address
		$this->subject = 'Email from >>your website<<'; // the subject of the email 
		$this->error;
	
		// if name contains is less than 2 characters
		if (strlen($this->from_name) < 2) {
			$_SESSION['error'][0] = 'You must write a name.';
			$this->error = true;
		}
		
		// if email ! is not valid
		if (!preg_match('/^([a-z0-9][-a-z0-9_\.]+|\*)@([a-z0-9][-a-z0-9_\.]+|\*)\.[a-z]{2,6}/i', $this->from_email)) {
			$_SESSION['error'][1] = 'You must write a valid email address.';
			$this->error = true;
		}
	
		// if message contains less than 10 characters
		if (strlen($this->message) <= 10) {
			$_SESSION['error'][2] = 'The message must contain atleast 10 character.';
			$this->error = true;
		}
		
		// if message doesn't end with a #, it will be marked as spam
		if (substr($this->message, -1) != '#') {
			$_SESSION['error'][3] = 'You didn\'t finish your message with a #, if you don\'t it will be marked as spam.';
			$this->error = true;
		}
	
		$_SESSION['info']['name'] = $this->from_name;
		$_SESSION['info']['email'] = $this->from_email;
		$_SESSION['info']['message'] = $this->message;
	
		if (!$this->error) {

			if (get_magic_quotes_gpc()) {
				$this->from_name = stripslashes($this->from_name);
				$this->from_email = stripslashes($this->from_email);
				$this->message = stripslashes($this->message);
			}
	
			mail($this->to, $this->subject, $this->message, 'From: '.$this->from_name.' <'.$this->from_email.'>');
			unset($_SESSION['info']);
			
			header('location: contact.php?done=true');
	
		}
		
	}
		
}

if ($_POST['contact'] == 'send') {
	$contact = new contact();
	$contact->sendEmail($_POST['from_name'], $_POST['from_email'], $_POST['message']);
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sv" lang="sv">

<head>
	<title>contact form</title>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<meta http-equiv="cache-control" content="no-cache,no-store,must-revalidate,max-age=0" />
	<meta http-equiv="pragma" content="no-cache" />
	<meta name="generator" content="" />
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	
	<style type="text/css">

	* {
		margin: 0px;
		padding: 0px;
	}

	fieldset {
		border: 1px solid #999;
		width: 600px;
		padding: 20px 0px;
	}
	
	fieldset legend {
		margin-left: 15px;
		padding: 2px 6px;	
	}
	
	fieldset label {
		width: 150px;
		float: left;
		text-align: right;
		margin-right: 10px;
		display: block;
		margin-top: 9px;
	}
	
	p.margin {
		margin-left: 155px;
	}
	
	#error_box {
		padding-bottom: 15px; 
		margin-left: 175px;
	}
	
	#error_box .topic {
		list-style-type: none; 
		font-weight: bold; 
		color: red;	
	}
	
	</style>

</head>

<body>

<?
if (empty($_GET['done'])) {
?>
<p style="padding-bottom: 5px;">
You must end your message with a #. This is to prevent spam.
</p>

<form action="contact.php" method="post">
<input type="hidden" name="contact" value="send" />

<fieldset>
<legend>contact</legend>
		
<?
if (isset($_SESSION['error']) && $_POST['contact'] == 'send') {
	echo '<ul id="error_box">'."\n";
	echo '<li class="topic">Something went wrong:</li>'."\n";
	foreach($_SESSION['error'] as $key => $value) {
		echo '<li >'.$value.'</li>'."\n";
	}
	echo '</ul>'."\n";
}
?>
<p><label for="name">Name:</label><input type="text" id="from_name" name="from_name" style="margin: 5px 0px;" value="<?=$_SESSION['info']['name']?>" /></p>
<p><label for="email">Email:</label><input type="text" id="from_email" name="from_email" style="margin: 5px 0px;" value="<?=$_SESSION['info']['email']?>" /></p>
<p><label for="message">Message:</label></p>
<p><textarea name="message" id="message" style="margin: 5px 0px; width: 400px; height: 180px;"><?=$_SESSION['info']['message']?></textarea></p>
<p class="margin"><input type="submit" name="submit" value="Contact me" style="margin: 5px 0px;" /></p>

</fieldset>
</form>

<?
} else if ($_GET['done']) {
	echo '<p>Message was successfully sent!</p>'."\n";		
}
?>

</body>
</html>
262 ms totalt · 4 externa anrop · v20260731065814-full.6fe65c25
129 ms — deklarationer (db)
0 ms — hämta statistik (cache)
131 ms — hämta tråd, inlägg och bilagor (db)
126 ms — ändringar (db)