HTML表单返回原始PHP代码

时间:2013-07-17 15:28:33

标签: php html forms

我有一个表单,我希望将内容邮寄到一个电子邮件地址,我遇到的问题是,只要我点击提交按钮,页面就会显示原始的PHP代码而且没有电子邮件是被送了。我拿出了电子邮件地址,但我现在只有一个雅虎帐户用于测试目的。我通过安装了Wordpress的WAMP在本地运行,并且没有Wordpress的问题。有什么想法吗?

HTML

<form id="signup" autocomplete="on" method="post" name="contactform" action="http://localhost/contact-form-handler.php">
<div id="prayerRequest"></div><!-- END prayerRequest -->
<textarea class="text" name="prayer" placeholder="* Prayer Request" required=""></textarea><br>
<select name="title" class="dropDown" required="">
    <option disabled="disabled" selected="selected">* Title</option>
    <option value="Mr">Mr.</option>
    <option value="Ms">Ms.</option>
    <option value="Mrs">Mrs.</option>
    <option value="Dr">Dr.</option>
    <option value="Rev">Rev.</option>
    <option value="Pastor">Pastor</option>
</select>
<input type="text" class="multiple" name="first" placeholder="* First Name" required="">
<input type="text" class="multiple" name="last" placeholder="* Last Name" required=""><br>
<input type="email" class="full" name="email" placeholder="* Email Address" required=""><br>
<input type="text" class="full" name="address" placeholder="* Street Address 1" required="">
<input type="text" class="full" placeholder="Street Address 2"> 
<input type="text" class="multiple" name="city" placeholder="* City" required="">
<button type="submit" class="submit">Submit Prayer Request</button>
<p>*Required Field</p>  

PHP

<?php 
$errors = '';
$myemail = 'myemail@domain.com';//<-----Put Your email address here.
if(empty($_POST['first'])  || 
empty($_POST['email']) || 
empty($_POST['prayer']))
{
$errors .= "\n Error: all fields are required";
}

$name = $_POST['first']; 
$email_address = $_POST['email']; 
$message = $_POST['prayer']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail; 
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $first \n Email: $email_address \n Message \n        $prayer"; 

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>

</body>
</html>

0 个答案:

没有答案