有人请帮助我编写代码,一切似乎都很好,但是,当我单击提交按钮时,浏览器会显示 PHP 脚本,而不是将电子邮件发送到我的邮件帐户。安装了 PHP 我认为这是问题所在。请帮忙??????这是我的 PHP 脚本
<?php
if(isset($_POST['submit'])) {
$email_to = "example@domain.com";
$email_subject = "Wedding Wishes";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['txtName']) ||
!isset($_POST['txtSurname']) ||
!isset($_POST['txtEmail']) ||
!isset($_POST['txtRegion']) ||
!isset($_POST['cmbRelation']) ||
!isset($_POST['txtMessage'])) {
died('We are sorry, but there appears to be a problem with the form you');
}
$first_name = $_POST['txtName']; // required
$last_name = $_POST['txtSurname']; // required
$email_from = $_POST['txtEmail']; // required
$region = $_POST['txtRegion'];
$relation = $_POST['txtRelation'];// not required
$message = $_POST['txtMessage']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Region: ".clean_string($region)."\n";
$email_message .= "Relation: ".clean_string($relation)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
而我的 Html 形式如下。
<div class="grid_16" style="width:400px;">
<div class="top-1">
<form id="form" method="post" action="SendEmail_Wishes.php" style="width: 400px;" >
<fieldset>
<p>
<label><strong>Name:</strong>
<input type="text" name="txtName" style= "width:300px; height:25px;">
<strong class="clear"></strong></label>
<label><strong>Surname:</strong>
<input type="text" name="txtSurname" style= "width:300px; height:25px;">
<strong class="clear"></strong></label>
<label><strong>Email:</strong>
<input name="txtEmail" type="text" style= "width:300px; height:25px;">
<strong class="clear"></strong></label>
<label><strong>Region:</strong>
<input name="txtRegion" type="text" id="txtRegion" value="Country & City" >
<strong class="clear"></strong></label>
<label><strong>Relation:</strong>
<select name="cmbRelation" id="cmbRelation" accesskey="2" tabindex="2">
<option value="Family">Family</option>
<option value="Friend" selected>Friend</option>
</select>
<br>
</label>
<label><strong>Message:</strong>
<textarea name="txtMessage" id="txtMessage" style= "width:300px;"></textarea>
<font size="1"><br />
<br />
<br />
<br />
<br />
<br />
(Maximum characters: 250)<br>
<input name="submit" type="submit" value="submit" style="width:50px;">
<br />
<strong class="clear"></strong></label>
</p>
<script type="text/javascript">
function blank(a) { if(a.value == a.defaultValue) a.value = ""; }
function unblank(a) { if(a.value == "") a.value = a.defaultValue; }
</script>
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
</fieldset>
</form>